Wednesday, 4 May 2016

Cross Browser Testing using Selenium

What is Cross Browser Testing?

Cross Browser Testing is a type of functional test to check that your web application works as expected in different browsers.

Why do we need Cross Browser Testing?

Web based applications are totally different from windows applications. A web application can be opened in any browser by the end user. For example some people prefer to open http://twitter.comin Firefox browser, while other's can be using Chrome browser or IE.
In the diagram below you can observe that in IE, the login box of twitter is not showing curve at all corners but we are able to see it in chrome browser.

    
So we need to ensure that the web application will work as expected in all popular browsers, so that more people can access it and use it.
This motive can be fulfilled with Cross Browser Testing of the product.
Reason Cross Browser Issues
  1. Font size mismatch in different browsers.
  2. JavaScript implementation can be different.
  3. CSS,HTML validation difference can be there.
  4. Some browser still not supporting HTML5.
  5. Page alignment and div size.
  6. Image orientation.
  7. Browser incompatibility with OS. Etc.

How to perform Cross Browser Testing

If we are using Selenium WebDriver, we can automate test cases using Internet Explorer, FireFox, Chrome, Safari browsers.
To execute test cases with different browsers in the same machine at same time we can integrate TestNGframework with Selenium WebDriver.
Your testing.xml will look like that,
This testing.xml will map with the test case which will look like that
Here because the testing.xml has two Test tags ('ChromeTest','FirefoxTest'),this test case will execute two times for 2 different browsers.
First Test 'ChromeTest' will pass the value of parameter 'browser' as 'chrome' so ChromeDriver will be executed. This test case will run on Chrome browser.
Second Test 'FirefoxTest' will pass the value of parameter 'browser' as 'firefox' so FirefoxDriver will be executed. This test case will run on FireFox browser.
Complete Code:
Guru99CrossBrowserScript.java
?
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package parallelTest;
 
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.testng.annotations.BeforeTest;
 
import org.testng.annotations.Parameters;
 
import org.testng.annotations.Test;
 
public class Guru99CrossBrowserScript {
 
    WebDriver driver;
 
     
 
    /**
 
     * This function will execute before each Test tag in testng.xml
 
     * @param browser
 
     * @throws Exception
 
     */
 
@BeforeTest
 
    @Parameters("browser")
 
    public void setup(String browser) throws Exception{
 
        //Check if parameter passed from TestNG is 'firefox'
 
        if(browser.equalsIgnoreCase("firefox")){
 
        //create firefox instance
 
            driver = new FirefoxDriver();
 
        }
 
        //Check if parameter passed as 'chrome'
 
        else if(browser.equalsIgnoreCase("chrome")){
 
            //set path to chromedriver.exe You may need to download it from http://code.google.com/p/selenium/wiki/ChromeDriver
 
            System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
 
            //create chrome instance
 
            driver = new ChromeDriver();
 
        }
 
else if(browser.equalsIgnoreCase("ie")){
 
            //set path to IEdriver.exe You may need to download it from
 
     // 32 bits http://selenium-release.storage.googleapis.com/2.42/IEDriverServer_Win32_2.42.0.zip
 
     // 64 bits http://selenium-release.storage.googleapis.com/2.42/IEDriverServer_x64_2.42.0.zip
 
            System.setProperty("webdriver.ie.driver","C:\\IEdriver.exe");
 
            //create chrome instance
 
            driver = new InternetExplorerDriver();
 
        }
 
        else{
 
            //If no browser passed throw exception
 
            throw new Exception("Browser is not correct");
 
        }
 
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 
    }
 
     
 
    @Test
 
    public void testParameterWithXML() throws InterruptedException{
 
        driver.get("http://demo.guru99.com/V4/");
 
        //Find user name
 
        WebElement userName = driver.findElement(By.name("uid"));
 
        //Fill user name
 
        userName.sendKeys("guru99");
 
        //Find password
 
        WebElement password = driver.findElement(By.name("password"));
 
        //Fill password
 
        password.sendKeys("guru99");
 
    }}
testing.xml
?
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
<?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="parallelTest.CrossBrowserScript">
 
</class>
 
</classes>
 
</test>
 
<test name="FirefoxTest">
 
<parameter name="browser" value="Firefox" />
 
<classes>
 
<class name="parallelTest.CrossBrowserScript">
 
</class>
 
</classes>
 
</test>
 
<test name="IETest">
 
<parameter name="browser" value="IE" />
 
<classes>
 
<class name="parallelTest.CrossBrowserScript">
 
</class>
 
</classes>
 
</test>
 
</suite>

6 comments:

  1. My friend Suggest me this blog and I can say this is the best blog to get the basic knowledge.Thank you so much for this Selenium Training in Chennai blog

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Thanks for the great information ,was looking for this information from long.Great blog SAP Basis Training in Chennai

    ReplyDelete
  4. Best blog.Got to learn new things.Thanks for this Blog SAP HR Training in Chennai blog

    ReplyDelete
  5. Great place to learn about the new things and got great knowledgeSAP MM Training in Chennai

    ReplyDelete
  6. Great place to learn about the new things and got great knowledgeSAP MM Training in Chennai

    ReplyDelete