Cross browser testing - Selenium with IE, Chrome and Mozilla browser

Testing your web application across different browsers to make sure:
  • Application performs similar manner for different browsers.
  • Webelements should be working as expected.(Active or inactive)
  • Application should have same font and color for objects.
  • Length/width of fields should be same across browsers.
Test case: log in to mercury tour website and verify the page title.

For this we will use selenium java and testNG framework.
  • Use testng.xml to send parameters as firefox / ie / chrome, to the selenium code for the testcase to execute on any one of the browser.
  • Make sure to have some value to parallel attribute in testng.xml.
  • Place chromedriver.exe and IEDriverServer.exe under your project folder for ease of access.
Notes for the Selenium with IE browser:
  • Make sure the browser zoom level is set to 100%.
  • Protected mode setting should be wither enable / disable for all the zones (Internet, local inranet, trusted sutes and restricted sites), can be accessible by navigating to IE browser manubar Tools -> Internet Options -> Security Tab.
  • Add few lines before declaring the InternetExplorerDriver                                                           
    File file = new file("./IEDriverServer.exe"); //path of IEDriverServer.exe
    System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
    driver = new InternetExplorerDriver();
Test case: log in to mercury tour website and verify the page title.

Selenium code should look like:
Created a package as mypckg and class as multibrowser.java for the below code


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package mypckg;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.*;

public class multibrowser {

    public WebDriver driver; //declared outside, so each  method in class can access
    
    @Parameters("browser") //parameter is a tag with attribute name = "browser" in testng.xml 
    @BeforeTest  //BeforeTest will execute before every Test(method) executes              
    public void setup(String browser) throws Exception {
        if(browser.equalsIgnoreCase("firefox"))
            {driver = new FirefoxDriver();}
        else if(browser.equalsIgnoreCase("ie"))
            { File file = new file("./IEDriverServer.exe");
              System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
            driver = new InternetExplorerDriver();}
        else if(browser.equalsIgnoreCase("chrome"))
            {System.setProperty("webdriver.chrome.driver","./chromedriver.exe");
            driver=new ChromeDriver();}
        else
            throw new Exception("browser name is not valid!");
    }
    
    @Test
    public void MercuryTourLogin()
    {
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://newtours.demoaut.com/");
        
        WebElement uname = driver.findElement(By.xpath("//input[@name='userName']"));
        uname.sendKeys("mercury");
        WebElement password = driver.findElement(By.xpath("//input[@name='password']"));
        password.sendKeys("mercury");
        driver.findElement(By.xpath("//input[@name='login']")).click();
        
        String title = driver.getTitle();
        if (title.contains("Find a Flight"))
        {
            System.out.println("test case: PASS");
        }        
    }
    
}


testNG.xml should be something like:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="TestSuite" thread-count="2" parallel="tests" >
<test name="ChromeTest">
    <parameter name="browser" value="Chrome" />
    <classes>
        <class name="mypckg.multibrowser"/>
    </classes>
</test>
<test name="FirefoxTest"> 
    <parameter name="browser" value="Firefox" />
    <classes>
        <class name="mypckg.multibrowser"/>
    </classes>
</test>
<test name="IETest">
    <parameter name="browser" value="IE" />
    <classes>
        <class name="mypckg.multibrowser"/>
    </classes>
</test>
</suite>


Now execute the code - Right click on the testng.xml, select "Run As" -> "TestNG Suite" and you are done!

No comments:

Post a Comment