如何运行具有不同所需功能的多个Selenium测试

时间:2013-02-15 12:23:16

标签: unit-testing testing selenium

我想运行相同的测试代码:

e.g。

WebElement element = driver.findElement(By.name("q"));
element.sendKeys("mysql excel 2013");
element.submit();

// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());

// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver d) {
        return d.getTitle().toLowerCase().startsWith("mysql");
    }
});

// Should see: "cheese! - Google Search"
System.out.println("Page title is: " + driver.getTitle());
// Close the browser

使用不同的功能配置

DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setVersion("15");
capability.setCapability("flash", "11-4");

RemoteWebDriver driver =  new RemoteWebDriver(new URL("http://192.168.1.22:4444/wd/hub"), capability);

DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setVersion("21");
capability.setCapability("flash", "11-4");

RemoteWebDriver driver =  new RemoteWebDriver(new URL("http://192.168.1.22:4444/wd/hub"), capability);

如果不创建具有不同功能的多个实例,最好的方法是什么? Selenium是否提供了这样做的概念?

或者我会“简单地”使用某种循环结构来循环?

1 个答案:

答案 0 :(得分:3)

为了达到这个目的,你应该使用testng

来寻找参数传递概念

TestNg Suite

  <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite thread-count="2" name=MyTestSuite" parallel="tests">

      <test name="RunInFirefox" preserve-order="false">
       <parameter name="version" value="8"/>
       <parameter name="browser" value="firefox"/>
       <parameter name="flashVersion" value="11.2"/>
        <classes preserve-order="true">
           <class name="com.test.TestCase1"/>
           <class name="com.test.TestCase2"/>
           <class name="com.test.TestCase3"/>
        </classes>
      </test>

     <test name="RunInChrome" preserve-order="false">
       <parameter name="version" value="21"/>
       <parameter name="browser" value="chrome"/>
       <parameter name="flashVersion" value="11.2"/>
        <classes preserve-order="true">
           <class name="com.test.TestCase1"/>
           <class name="com.test.TestCase2"/>
           <class name="com.test.TestCase3"/>
        </classes>
      </test>
    </suite>

TestNg TestCase

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class ExampleTestCase {

private static WebDriver driver;

@Parameters({"browser,version,flashVersion"})
@BeforeClass
public void beforeClass(String browser,String version,String flashVersion) 
{
    driver=getDriverInstance(browser,version,flashVersion);
    }

@BeforeTest
public void beforeTest() 
{

}
@Test
public void f() 
{
    //your test code here
}

@AfterTest
public void afterTest() 
{

}

@AfterClass
public void afterClass() 
{

}

}

getDriverInstance 方法隐含

public static WebDriver getDriverInstance(String browser,String version,String flashVersion)
{
    WebDriver driver=null;
    if(browser.equals("firefox"))
    {
        DesiredCapabilities capability = DesiredCapabilities.firefox();
        capability.setVersion(version);
        capability.setCapability("flash", flashVersion);
        driver =  new RemoteWebDriver(new URL("http://192.168.1.22:4444/wd/hub"), capability);
    }
    else if(browser.equals("chrome"))
    {
        DesiredCapabilities capability = DesiredCapabilities.chrome();
        capability.setVersion(version);
        capability.setCapability("flash", flashVersion);
        driver =  new RemoteWebDriver(new URL("http://192.168.1.22:4444/wd/hub"), capability);
    }
    return driver;
}