如何将当前功能传递给单个测试

How do I pass the current capabilities to individual tests

这可能就像从另一个 class 获取变量一样简单。我还在学习 Java 和 Selenium。

我希望测试 运行 报告 (ExtentReports) 能够在 @Test 级别(功能)报告浏览器。目前Grid 运行在不同的浏览器上进行了相同的测试,报告没有区分。

使用 Selenium Grid,我使用 @BeforeMethod 定义了我的 @Test 功能(包括浏览器)。我在我的 BaseTest class.

中这样做
public class BaseTest {
@BeforeMethod(alwaysRun = true)
@Parameters({ "platform", "browser", "version" })
public void setup(String platform, String browser, String version)
        throws MalformedURLException, InterruptedException {    
    RemoteWebDriver driver = null;      
    //important: Thread local!
    threadedDriver = new ThreadLocal<RemoteWebDriver>();
    DesiredCapabilities caps = new DesiredCapabilities();
    // Platforms
    if (platform.equalsIgnoreCase("windows"))
        caps.setPlatform(Platform.WINDOWS);
    if (platform.equalsIgnoreCase("XP"))
        caps.setPlatform(Platform.XP);
    if (platform.equalsIgnoreCase("WIN8"))
        caps.setPlatform(Platform.WIN8);
    if (platform.equalsIgnoreCase("WIN8_1"))
        caps.setPlatform(Platform.WIN8_1);
    if (platform.equalsIgnoreCase("ANY"))
        caps.setPlatform(Platform.ANY);
    if (platform.equalsIgnoreCase("MAC"))
        caps.setPlatform(Platform.MAC);
    if (platform.equalsIgnoreCase("Android"))
        caps.setPlatform(Platform.ANDROID);
    // Browsers
    if (browser.equalsIgnoreCase("Internet Explorer"))
        caps.setBrowserName("internet explorer");
    if (browser.equalsIgnoreCase("Firefox"))
        caps.setBrowserName("firefox");
    if (browser.equalsIgnoreCase("chrome"))
        caps.setBrowserName("chrome");
    if (browser.equalsIgnoreCase("MicrosoftEdge"))
        caps.setBrowserName("MicrosoftEdge");
    if (browser.equalsIgnoreCase("iPad"))
        caps.setBrowserName("ipad");
    if (browser.equalsIgnoreCase("iPhone"))
        caps.setBrowserName("iphone");
    if (browser.equalsIgnoreCase("Android"))
        caps.setBrowserName("android");
    // Version
    caps.setVersion(version);
    System.out.println(caps);
    System.out.println(browser);
    //Initialize driver with capabilities
    driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), caps);

    //this uses below methods to set above RemoteWebDriver to the getDriver()
    //method in a threaded instance.
    setWebDriver(driver);
    initialize();

}
}

所以现在我有一个 browser 每个线程网格测试本地变量 运行。我需要将该变量放入每个 @Test 方法中。这是我在单独的 class 中的 @Test。在 try 语句的开头,我想打印 browser 变量 用于当前线程网格测试功能

public class Workflow1 extends BaseTest {
@Test
public void Workflow1TestInvalidPolicyNumbers() throws InterruptedException {
    HomePage homePage = new HomePage(getDriver());
    ExtentTest testReporter = ComplexReportFactory.getTest();
    try {
System.out.println("This is the browser:" + ??(help here)??);
        loginMethod("TestUser","TestPassword");         
        homePage.setFindAPersonOrPolicySearchField("1234");
        homePage.clickSearchButton();
        testReporter.log(LogStatus.INFO, "Searched \"1234\"");
        Thread.sleep(2000);
        if (getDriver().getPageSource().contains("Policy numbers should be 7 or 10 digits long"))
            testReporter.log(LogStatus.PASS, "Policy numbers should be 7 or 10 digits long");
        else 
            testReporter.log(LogStatus.FAIL, "Results incorrect" + testReporter.addScreenCapture(ComplexReportFactory.CaptureScreen(getDriver())));

    } catch (Exception e) {
        testReporter.log(LogStatus.ERROR, "Exception found: " + e.getMessage()
                + testReporter.addScreenCapture(ComplexReportFactory.CaptureScreen(getDriver())));
        System.out.println(e);
    }
}

好像是浏览器信息传入setup()方法中的BaseTest.javaclass

您可以将此数据存储在一个变量中,然后所有相关的 classes:

都可以使用该变量
public class BaseTest {

protected String browser; // add a property to hold this value

@BeforeMethod(alwaysRun = true)
@Parameters({ "platform", "browser", "version" })
public void setup(String platform, String browser, String version)
        throws MalformedURLException, InterruptedException {   
    this.browser = browser; // store the given browser string
    RemoteWebDriver driver = null;      
    //important: Thread local!
    threadedDriver = new ThreadLocal<RemoteWebDriver>();
    DesiredCapabilities caps = new DesiredCapabilities();
    // Platforms
    if (platform.equalsIgnoreCase("windows"))
        caps.setPlatform(Platform.WINDOWS);

然后 BaseTest subclasses 可以直接引用它:

public class Workflow1 extends BaseTest {
@Test
public void Workflow1TestInvalidPolicyNumbers() throws InterruptedException {
    HomePage homePage = new HomePage(getDriver());
    ExtentTest testReporter = ComplexReportFactory.getTest();
    try {
    System.out.println("This is the browser:" + browser); // then retrieve it

您还可以通过查询 RemoteWebDriver 对象本身来获取浏览器风格名称和一大堆信息,以通过调用

揭示实际功能
getDriver().getCapabilities().getBrowserName()

这甚至不需要在测试中将浏览器风格作为单独的数据成员class。

请参阅 here 获取 javadoc。