Tuesday, September 27, 2016

MultiBrowser Testing

MultiBrowser Testing

Users can execute scripts in multiple browser simultaneously. For demo purposes, we will make use of the same scenario that we had taken for Selenium Grid. In selenium Grid example, we had executed scripts remotely, where as we will execute the scripts locally.

Even for this we should ensure that we have appropriate drivers downloaded. Please refer to Selenium Grid Chapter for downloading IE and Chrome Drivers.

Example

We will perform percent calculator in all browsers simulataneously for demo purposes.

package TestNG; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.*; import org.testng.annotations.*; public class TestNGClass { private WebDriver driver; private String URL = "http://www.calculator.net"; @Parameters("browser") @BeforeTest public void launchapp(String browser) { if (browser.equalsIgnoreCase("firefox")) { System.out.println(" Executing on FireFox"); driver = new FirefoxDriver(); driver.get(URL); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); } else if (browser.equalsIgnoreCase("chrome")) { System.out.println(" Executing on CHROME"); System.out.println("Executing on IE"); System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe"); driver = new ChromeDriver(); driver.get(URL); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); } else if (browser.equalsIgnoreCase("ie")) { System.out.println("Executing on IE"); System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe"); driver = new InternetExplorerDriver(); driver.get(URL); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); } else { throw new IllegalArgumentException("The Browser Type is Undefined"); } } @Test public void calculatepercent() { driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click(); // Click on Math Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click(); // Click on Percent Calculators driver.findElement(By.id("cpar1")).sendKeys("10"); // Enter value 10 in the first number of the percent Calculator driver.findElement(By.id("cpar2")).sendKeys("50"); // Enter value 50 in the second number of the percent Calculator driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click(); // Click Calculate Button String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText(); // Get the Result Text based on its xpath System.out.println(" The Result is " + result); //Print a Log In message to the screen if(result.equals("5")) { System.out.println(" The Result is Pass"); } else { System.out.println(" The Result is Fail"); } } @AfterTest public void closeBrowser() { driver.close(); } }

Create an XML which will help us in parameterizing Browser name and don't forget to mention parallel="tests" inorder to execute in all browsers simultaneously.

Execute the script by performing right click on the XML file and select 'Run As' >> 'TestNG' Suite as shown below.

Output

All Browser would be launched parallely and the result would be printed on console.

Note : For us to execute on IE successfully ensure that the check box 'Enable Protected Mode' under security Tab of 'IE Option' is either checked or Unchecked across all zones.

TestNG results can be viewed in HTML format for detailed analysis.

1 comment:

  1. You made some decent factors there about Mobile App Testing Services. I looked on the internet for the difficulty and found most individuals will associate with along with your website. Keep update more excellent posts on Mobile Testing Services

    ReplyDelete