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を使用したパラメーター受け渡しの概念に進む必要があります

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