2

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

例如

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 是否为此提供了一个概念?

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

4

1 回答 1

3

为了实现这一点,您应该使用 testng 进行参数传递概念

测试套件

  <?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 测试用例

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;
}
于 2013-02-15T14:53:15.483 回答