Friday, 15 November 2013

Multi Browser Testing using Selenium TestNG

This Section describes how to run your Test cases on different browsers.

Few Simple steps Using TestNG :)

Step 1: Create your Script. Using TestNG annotations. Define parameters (using @Parameters) for taking input value i.e, which browser should be used for Running the Test

Step 2: Create a TestNG XML for running your script

Step 3: Configure the TestNG XML for passing parameters i.e, to tell which browser should be used for Running the Test

Step 4: Run the TestNG XML which can pass the appropriate browser name to the Script such that the Test Case is executed in a specified browser


Programatically this can be Done as follows:

Step 1:


package atu.multibrowser;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class MultiBrowserTest {

       private WebDriver driver;

       // Configure for multi browser drivers
       @Parameters("browser")
       @BeforeClass
       public void beforeTest(String browser) {
              if (browser.equalsIgnoreCase("firefox")) {
                     driver = new FirefoxDriver();
              } else if (browser.equalsIgnoreCase("chrome")) {
                     // Set Path for the executable file
                     System.setProperty("webdriver.chrome.driver",
                                  "D:\\chromedriver.exe");
                     driver = new ChromeDriver();
              } else if (browser.equalsIgnoreCase("ie")) {
                     // Set Path for the executable file
                     System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
                     driver = new InternetExplorerDriver();
              } else {
                     throw new IllegalArgumentException("The Browser Type is Undefined");
              }
              // Open App
              driver.get("http://demo.opensourcecms.com/wordpress/wp-login.php");
       }

       @Test
       public void login() throws InterruptedException {
              // Enter UserName
              driver.findElement(By.id("user_login")).clear();
              driver.findElement(By.id("user_login")).sendKeys("admin");
              // Enter Password
              driver.findElement(By.id("user_pass")).clear();
              driver.findElement(By.id("user_pass")).sendKeys("demo123");
              // Click on Submit button
              driver.findElement(By.id("wp-submit")).submit();
       }

       @AfterClass
       public void afterTest() {
              try {
                     driver.quit();
              } catch (Exception e) {
                     driver = null;
              }
       }
}




Step 2 & Step 3: The below XML is configured to run the Test Case in Firefox, Chrome and IE browser in sequential manner

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">

       <test name="FirefoxTest">
              <parameter name="browser" value="firefox" />
              <classes>
                     <class name="atu.multibrowser.MultiBrowserTest" />
              </classes>
       </test>


       <test name="ChromeTest">
              <parameter name="browser" value="chrome" />
              <classes>
                     <class name="atu.multibrowser.MultiBrowserTest" />
              </classes>
       </test>


       <test name="IETest">
              <parameter name="browser" value="ie" />
              <classes>
                     <class name="atu.multibrowser.MultiBrowserTest" />
              </classes>
       </test>

</suite>



Step 4:

Run the above XML as TestNG Suite from Eclipse IDE as shown below.






/***********************************************************************/

Enhancing your TestNG xml for running the Test Case on Different browsers Simultaneously. Speeding up the execution process :)

Using the feature provided by TestNG for Parallel Executions. Set the"parallel" attribute to "tests" so that the all the three browser tests can be executed Simultaneously.



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<!--Change The Parallel attribute for parallel Execution-->
<suite name="Suite" parallel="tests">

       <test name="FirefoxTest">
              <parameter name="browser" value="firefox" />
              <classes>
                     <class name="atu.multibrowser.MultiBrowserTest" />
              </classes>
       </test>


       <test name="ChromeTest">
              <parameter name="browser" value="chrome" />
              <classes>
                     <class name="atu.multibrowser.MultiBrowserTest" />
              </classes>
       </test>


       <test name="IETest">
              <parameter name="browser" value="ie" />
              <classes>
                     <class name="atu.multibrowser.MultiBrowserTest" />
              </classes>
       </test>

</suite>


This is one short and quick way of setting up Multiple Browser Testing :)

40 comments:

  1. Can we USE ur AUT reports for this test suite execution?????

    ReplyDelete
    Replies
    1. Hey AUT - I found this comes under suite folder thanks

      Delete
  2. is it possible to have 2 suite's in single xml file or i need to have separate for each set .

    ReplyDelete
    Replies
    1. According to TestNG documentation, you can call multiple suite files in a single suite file.

      #AT

      Delete
  3. For Single test(I mean for single class inside the test), the above testng will work fine but not for multiple classes inside the test
    example:




    Issue: Chrome browser stops execution once the Firefox opens

    ReplyDelete
    Replies
    1. Example :
      classes>
      class name="atu.multibrowser.MultiBrowserTest_1" />
      class name="atu.multibrowser.MultiBrowserTest_2" />
      /classes>
      Note:I removed the "<" since I was not able to see after publish

      Delete
    2. Hi Pradeep,

      The post you referred explains more about multi browser tests - TestNG can also run methods in parallel. Please refer to TestNG documentaion.

      #AT

      Delete
  4. For IE I am getting error:

    org.openqa.selenium.NoSuchElementException: Unable to find element with id == user_login (WARNING: The server did not provide any stacktrace information)

    ReplyDelete
    Replies
    1. Seems that it's wrong locator for user_login field in IE. Open your website in IE and check path to this field.

      Delete
    2. I am get the same error when i launch 3 diff browsers like IE , FF , Chrome browsers the execution happens only on FF and rest of the two will be just still - hence we end up unable to find element errors. Btw I also checked that the locator is correct on IE / FF / chrome (its consistent).

      Delete
  5. Automation Tester, good sample! Thank you =) How do you think, it will work with relative path to chromedriver.exe? I want to load my script to server and don't want care about strong path to drivers.

    ReplyDelete
    Replies
    1. It should work fine ! having the exe files relative to your project folder structure.

      #AT

      Delete
  6. Hi,
    I'm getting below error , please help me on this.

    Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.

    Thanks,
    Anjaneyulu Vujani

    ReplyDelete
    Replies
    1. you need to set the security zones to same (either checked or unchecked) in IE. Go To Internet Options, under security tab, set them to equal for all the zones.

      #AT

      Delete
  7. This information which you provided is very much useful for us.It was very interesting and useful for qa online training.We also providing qa online training institute in worldwide.

    ReplyDelete
  8. Hi, am unable to run the suite,, can u help me please..

    ReplyDelete
  9. How to get screenshot when we come across any failure in AUT report ?

    ReplyDelete
  10. Thank you very much..i was keep on looking for this kind of example till evening, At last i got it thank you very much..

    Great Explanition

    ReplyDelete
  11. How to get screenshot when we come across any failure in AUT report ?

    ReplyDelete
  12. Thank you. As you said short and quick. I would say it is awesome.

    ReplyDelete
  13. Hi,

    I am following the same method for performing Cross browser testing where
    in the xml file i have mentioned parallel = "tests" and thread-count =
    "3" but, the main problem what i am facing is say i have configured for 3
    browsers (ff /ie / chrome) all the 3 browsers open and load the and URL but
    only my FF browser will be performing the execution rest of the two
    will not do anything. So could you please help me how to achieve
    parallel execution.
    BTW my browser versions are FF38.5 , IE 11 , Chrome49

    ReplyDelete
  14. Hi,

    I have ran a cross browser scenario on FF, Chrome and IE. After execution while i try to view the report, I have spotted 3 runs out of which Run 1 and Run 2 are not displaying any content with error page displayed as "Your file was not found, It may have been moved or deleted."

    Run 3 came up with the result of the execution of final browser among the three (Only the result of IE).

    Can you please suggest a possible way by which i will be able to view the report of all the browsers (IE, Chrome and FF) using ATU?

    ReplyDelete
  15. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium.
    Selenium

    ReplyDelete
  16. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium.
    Selenium

    ReplyDelete
  17. how to run this sample ...please describe step by step

    ReplyDelete
  18. Well explained. Got to learn new things from your Blog on Appium.Appium training in chennai

    ReplyDelete
  19. Hi,
    Thanks for providing sample project. :)

    ReplyDelete
  20. Nice Information about multi browser My sincere thanks for sharing this post Please Continue to share this post
    Selenium Training in Chennai

    ReplyDelete
  21. really nice blog has been shared by you. before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blogs.
    selenium training in chennai

    ReplyDelete
  22. This is an awesome post.Really very informative and creative contents.Thanks to sharing these concept is a good way to enhance my knowledge. I like this site very much.I like it and help me to develop my knowledge very well.Thank you for this brief explanation....
    Python Training in Chennai | Best Python Training in Chennai | Dot Net Training in Chennai

    ReplyDelete
  23. Hi, I am really happy to found such a helpful and fascinating post that is written in well understandable manner enhance me to learn quickly… Thanks.. Software Testing Training in Chennai | Selenium Training in Chennai



    ReplyDelete
  24. Hi, I am really happy to found such a helpful and fascinating post that is written in well understandable manner enhance me to learn quickly… Thanks..


    Selenium Training in Bangalore

    ReplyDelete
  25. This is a great post. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again.I really like this topic. Python Online Training
    Learn Python Online

    ReplyDelete