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 :)

61 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. how to run this sample ...please describe step by step

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

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

    ReplyDelete
  18. 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
  19. 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
  20. 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
  21. 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


  22. You've made some good points there. I looked on the internet for more information about this



    Mainframe Training In Chennai | Hadoop Training In Chennai | ETL Testing Training In Chennai

    ReplyDelete
  23. 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 for all zones.
    software testing training online

    ReplyDelete
  24. Great information thanks a lot for the detailed articleThat is very interesting I love reading and I am always searching for informative information like this.

    Online Robot Framework Training

    ReplyDelete
  25. I would like to say that this blog really convinced me to update my knowledge about the technology you talk about. Thanks,very good post.
    Selenium Training Chennai
    software testing selenium training
    selenium testing training in chennai
    Selenium Training in T Nagar
    Selenium Training in OMR

    ReplyDelete
  26. This excellent website truly has all of the info I wanted concerning this subject and didn’t know who to ask.
    Java Training in Bangalore
    Advanced Java Training in Bangalore

    ReplyDelete

  27. Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
    If you are looking for any Big data Hadoop Related information please visit our website Big Data Hadoop Training In Bangalore page!

    ReplyDelete
  28. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
    Digital Marketing Certification Training
    AWS Certification Training
    Python Certification Training
    Selenium Training
    Data Science Certification Training
    DevOps Certification Training

    ReplyDelete
  29. Very interesting blog. Learn the automation testing concept very well.Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.You can also check my articles as well.
    DevOps Training in Chennai

    DevOps Online Training in Chennai

    DevOps Training in Bangalore

    DevOps Training in Hyderabad

    DevOps Training in Coimbatore

    DevOps Training

    DevOps Online Training

    ReplyDelete
  30. Wow I can say that this is another great article as expected of this blog.Bookmarked this site.Thanks for sharing.Keep sharing more blogs like this.
    IELTS Coaching in chennai

    German Classes in Chennai

    GRE Coaching Classes in Chennai

    TOEFL Coaching in Chennai

    spoken english classes in chennai | Communication training


    ReplyDelete
  31. There's no indication that's the case. Looking at Twitter's public repositories on Github , Scala is still, by far, the most common language used. ... stuhood: Twitter is not moving away from Scala. Scala continues to grow at a faster rate internally then any other language, and is ~50% of Twitter's backend codebase.
    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java Training

    Java Online Training

    ReplyDelete
  32. Thanks for provide great informatic and looking beautiful blog, really nice required information & the things i never imagined and i would request, wright more blog and blog post like that for us. Thanks you...

    python training in chennai

    python course in chennai

    python online training in chennai

    python training in bangalore

    python training in hyderabad

    python online training

    python training

    python flask training

    python flask online training

    python training in coimbatore

    ReplyDelete
  33. The Blog contains the effective and attractive information’s and thanks for the blog.
    Java Training Institute in Chennai
    JAVA Training Institute in Bangalore

    ReplyDelete
  34. I started reading your post from the beginning, and it was quite interesting to read. I appreciate you providing such a nice blog, and I hope you continue to update it on a regular basis.

    advanced java online training

    ReplyDelete
  35. Simply try to say ones content material could as daze. This fathomability while your eloquent is extremely perfect and I could think youon an expert for this count. Google Earth Pro Free Key high-ecological components nearby close to than your settlement set aside in me to control off when your ongoing day have adequate confirmation to kid support changed by using fundamentally basically not pretty weblog message. thankful to you stacks loads of nearby near you ought to go upon the eye-getting amass the cycle wrapped up.

    ReplyDelete
  36. Good Night Quotes For Friends ... Part of me was wishing today would never end! Hanging out with you makes me feel young again. Good night friend of mine. I lookGood Night Friends Quotes

    ReplyDelete
  37. A great blog on multi browser testing using selenium software.

    ReplyDelete