如何在其他浏览器使用TestNg和eclipse完成之后的一个浏览器中如何在多个浏览器中运行Selenium Webdriver测试用例 [英] How to run Selenium webdriver test cases in multiple browser one after other browser get finish using TestNg and eclipse

查看:86
本文介绍了如何在其他浏览器使用TestNg和eclipse完成之后的一个浏览器中如何在多个浏览器中运行Selenium Webdriver测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在所有多个浏览器中运行selenium webdriver测试用例,但不能并行运行.是否可以在所有多个浏览器中运行测试用例而无需使用xml和硒网格.我希望我的测试用例应该先在firefox中执行,并在firefox中完成执行后,才应该在chrome中开始执行,等等.

I want to run selenium webdriver test cases in all multiple browser but not in parallel.Is it possible to run test cases in all multiple browser without using xml and selenium grid.Can we do it by using annotation and java classes.I wanted that my test cases should execute in firefox first and after completion of execution in firefox it should start execution in chrome and so on.

我已经尝试过这段代码,但是使用xml是并行执行的.

I have tried this code but execution is parallel by using xml.

CrossBrowserScript.java

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.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class CrossBrowserScript {

    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
            System.setProperty("webdriver.chrome.driver",".\\chromedriver.exe");
            //create chrome instance
            driver = new ChromeDriver();
        }
        //Check if parameter passed as 'IE'
                else if(browser.equalsIgnoreCase("ie")){
                    //set path to IE.exe
                    System.setProperty("webdriver.ie.driver",".\\IEDriverServer.exe");
                    //create IE 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");
    }
}

testngCrossBrowser.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="3"  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>

推荐答案

使用JUnit,您可以创建MethodRule( http://junit.org/apidocs/org/junit/rules/MethodRule.html ),它将在所有浏览器中运行测试.

With JUnit you can create MethodRule (http://junit.org/apidocs/org/junit/rules/MethodRule.html) which will run tests in all browsers.

示例:

public class ManyBrowsers implements MethodRule {

public static WebDriver driver;

@Override
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            //RUN FIREFOX
            driver = new FirefoxDriver();
            base.evaluate();
            driver.quit();

            //RUN CHROME
            File f = //PATH to CHROME DRIVER
            System.setProperty("webdriver.chrome.driver", f.getAbsolutePath());
            driver = new ChromeDriver();
            base.evaluate();
            driver.quit();
        }
    };
}

}

示例测试:

public class VisitGoogle {

@Rule
public ManyBrowsers browsers = new ManyBrowsers();

@Test
public void test() {
    ManyBrowsers.driver.navigate().to("https://www.google.com/");
}

}

这篇关于如何在其他浏览器使用TestNg和eclipse完成之后的一个浏览器中如何在多个浏览器中运行Selenium Webdriver测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆