0

Here is my script:

public class VerifyPageContent {

    @SuppressWarnings("unused")
    public static void main(String[] args) throws Exception {

        System.setProperty("webdriver.ie.driver", "C:\\Selenium\\Selenium Drivers\\IEDriverServer.exe");
        WebDriver driver = new InternetExplorerDriver();

        ExtentReports extent = new ExtentReports("C:/SeleniumDataAndScripts/Reports/qa/new.html", true);

        ExtentTest test = extent.startTest("Verify Page Content");

        extent.addSystemInfo("Environment" , "QA");


        //Opening Website and Capturing Screenshot

        driver.get("http://ww3.nedbank.co.za");
        driver.manage().window().maximize();



        //QA
                                            ////////////////////////////////////////////////////        
                                            ///VERIFY WEBSITE HEADER CONTENTS               ///
                                            ///////////////////////////////////////////////////     


        //NEDBANK LOGO  

        test.log(LogStatus.INFO, "Verifying 'Nedbank Logo' element");

           String expTextLogo = "Nedbank"; 

          String actTextLogo = driver.findElement(By.id("main-header")).getText();

            if(actTextLogo.contains(expTextLogo)){

                test.log(LogStatus.PASS, "Expected element '"+expTextLogo+"' is Present in the web page");
                System.out.println("0) Expected element '"+expTextLogo+"' is present in the web page.");
            }else{

                test.log(LogStatus.FAIL, "Expected element '"+expTextLogo+"' is not present in the web page");
                System.out.println("0) Expected element '"+expTextLogo+"' is not present in the web page.");

            }   

            extent.endTest(test);
            extent.flush();


        //APPLY ONLINE                       


        test.log(LogStatus.INFO, "Verifying Apply online element");

       String expText = "Apply online"; 

      String actText = driver.findElement(By.id("main-header")).getText();

        if(actText.contains(expText)){

            test.log(LogStatus.PASS, "Expected element '"+expText+"' is Present in the web page");
            System.out.println("1) Expected element '"+expText+"' is present in the web page.");
        }else{

            test.log(LogStatus.FAIL, "Expected element '"+expText+"' is not present in the web page");
            System.out.println("1) Expected element '"+expText+"' is not present in the web page.");

        }   

        extent.endTest(test);
        extent.flush();
2

Here is the general description of what you should do:

  1. Set up SeleniumGrid hub
  2. Set up nodes within different environments you would like to run your tests
  3. Isolate your test code within a separate method (of course it is better to use test frameworks like JUnit or TestNG). Your test method should take WebDriver as a parameter because you will have to re-instantiate drivers for different environments.
  4. Where you will call your test code you need to loop through your environment configurations which you will define via DesiredCapabilities class, create a RemoteWebDriver with those capabilities and call your test method sending your new WebDriver there.

This is basically how you will achieve running one test against several different environments.

So the code would look approximately like:

public static void main(String[] args) throws MalformedURLException {

    List<Environment> envs = new ArrayList<>();
    envs.add(new Environment(Platform.LINUX, BrowserType.CHROME));
    envs.add(new Environment(Platform.WINDOWS, BrowserType.CHROME));
    envs.add(new Environment(Platform.WINDOWS, BrowserType.EDGE));


    for(Environment env: envs){
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setBrowserName(env.getBrowserType());
        caps.setPlatform(env.getPlatform());
        WebDriver driver = new RemoteWebDriver(new URL("YOUR_URL"), caps);
        try{
            driver.get("http://ww3.nedbank.co.za");
            driver.manage().window().maximize();
            test(driver);
        }finally {
            driver.quit();
        }
    }

}

Where YOUR_URL is the URL of your SeleniumGrid hub and test(driver) is a method where you put all your test code. This code also uses one custom class that is defined as shown below:

public class Environment {

    Platform platform;
    String browserType;

    public Environment(Platform platform, String browserType) {
        this.platform = platform;
        this.browserType = browserType;
    }

    public Platform getPlatform() {
        return platform;
    }

    public String getBrowserType() {
        return browserType;
    }
}

This approach lets you add new environments or remove ones that are not required any more.

0

You can have multiple options to run the same scripts in different environments.

  • By using the selenium grid. Runs your tests across different environments parallel.
  • You can also try running the selenium on top of a Docker which will helps to run the script in different environments virtually. Try this link. After that in your script add the following line for executing in parallel.

driver = new RemoteWebDriver (new URL(“http://youripaddress:portnumber/wd/hub”), capabilities);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.