How to run Selenium tests in multiple browsers for cross-browser testing using Java?

前端 未结 4 1824
青春惊慌失措
青春惊慌失措 2020-12-06 08:37

I am using Selenium WebDriver with Java & TestNG framework. I want to use Firefox, IE, Chrome in one single code at a time for doing cross-browser testing. I can only in

相关标签:
4条回答
  • 2020-12-06 09:03

    For Ruby you can do the following if you want to run your single code in all three browsers one after the other:

    def all_browsers
      browsers = [:firefox,:ie,:chrome].each do |br|
      $driver = Selenium::WebDriver.for br
      $driver.manage.window.maximize
      $driver.navigate.to("http://google.com")
    end
    
    0 讨论(0)
  • 2020-12-06 09:13

    For Java, since the posters question was asking for a Java answer, I wrote a complete multi-browser parallel window example.

    0 讨论(0)
  • 2020-12-06 09:15

    For C#

    Add

    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Firefox;
    using OpenQA.Selenium.IE;
    

    Create a config file which will contain browser name. Implement a method for interaction with the config file

    And you can use below code for initialize browsers:

    string browser = GetConfigProperty("browser"); //Get browser name from the config
            switch (browser)
            {
                case "chrome":
                    driver = new ChromeDriver();
                    break;
                case "firefox":
                    driver = new FirefoxDriver();
                    break;
                case "InternetExplorer":
                    driver = new InternetExplorerDriver();
                    break;
            }
    

    good luck!

    0 讨论(0)
  • 2020-12-06 09:17

    Create testng.xml having the following content:

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    <suite name="Regression Test" verbose="1">  
        <test name="Login_IE">
        <parameter name="browser" value="iexplore"/>
            <classes>
                <class name="com.gb.test.selenium.login.Login" />
            </classes>
        </test>
        <test name="Login_FF">
        <parameter name="browser" value="firefox"></parameter>
        <classes>
        <class name="com.gb.test.selenium.login.Login" />
        </classes>
        </test>
    <test name="Login_Chrome">
        <parameter name="browser" value="chrome"></parameter>
        <classes>
        <class name="com.gb.test.selenium.login.Login" />
        </classes>
        </test>
    </suite>
    

    Now write your Java WebDriver code as below:

    @Parameters({"browser"})
      @BeforeClass
      public void setUp(String browser) {//throws MalformedURLException{
          if(browser.equalsIgnoreCase("iexplore")){
              File file = new File("E:\\IEDriverServer_Win32_2.29.1\\IEDriverServer.exe");
              System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
              driver = new InternetExplorerDriver();
    
          }
          if(browser.equalsIgnoreCase("firefox")){
              driver = new FirefoxDriver();
          }
          if(browser.equalsIgnoreCase("chrome")){
              File file = new File("F:\\chromedriver_win_22_0_1203_0b\\chromedriver.exe");//v22
            System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
            driver = new ChromeDriver();
          }
    
          jse = (JavascriptExecutor) driver;
          driver.manage().window().maximize();
          driver.manage().timeouts().implicitlyWait(DEFAULT_IMPLICIT_WAIT_IN_SECONDS, TimeUnit.SECONDS);
              driver.get("URLOfSite");
      }
    
    0 讨论(0)
提交回复
热议问题