繁体   English   中英

Selenium Java - 如何外包@Config 并从外部类调用测试?

[英]Selenium Java - How to outsource @Config and call tests from an extern class?

我敢肯定,有经验的 Java 开发人员可以很快回答这个问题。 但由于我对 Java 不太熟悉,我不知道如何在 Java 中获取 Selenium 的 @Config 部分。 如果我可以有一个配置文件或类,一方面可以将数据(浏览器、网站等)放在其中,另一方面可以放置测试文件,那将是最佳选择。
下面是一个测试文件的例子:

package com.example_test.selenium;

import io.ddavison.conductor.Browser;
import io.ddavison.conductor.Config;
import io.ddavison.conductor.Locomotive;
import org.junit.Test;

@Config(
        browser = Browser.CHROME,
        url     = "http://example.com"
)

public class test_a_Home extends Locomotive {
    @Test
    public void testifExists() {
        validatePresent(site_a_Home.EL_NEWCUSTOMERBANNER);
    }
}

现在我想要一个名为 tests.java 的单独文件,我可以在其中调用“test_a_Home”函数。 如果我尝试一下

package com.example_test.selenium;

public class tests {
    test_a_Home test = new test_a_Home();

    test.testifExists();

}

我收到错误,“testifExists()”无法解决。
我尝试将public void testifExists()更改为public int testifExists()并尝试使用int res = test.testifExists();调用它int res = test.testifExists(); class tests但这也不起作用,因为我收到错误java.lang.Exception: Method testNewCustomersBannerExists() should be void
如果有人可以帮助我,我会很高兴。 如果您需要更多信息,请随时提及。 谢谢你。

如果你希望你的设计是这样的,那么你需要这样组织你的测试:

public class BasePage {
    public Locomotive test;
    public BasePage(Locomotive baseTest) {
        test = baseTest;
    }
}

public class test_a_Home extends BasePage {
    public test_a_Home(Locomotive baseTest) {
        super(baseTest);
    }

    public void testifExists() {
        test.validatePresent(site_a_Home.EL_NEWCUSTOMERBANNER);
    }
}

然后是您的测试类,我建议您也创建一个基类:

@Config(
    browser = Browser.CHROME,
    url     = "http://example.com"
)
public class BaseTest extends Locomotive {}

然后你的测试类:

public class tests extends BaseTest {
    test_a_Home test = new test_a_Home(this);

    @Test
    public void testHomePage() {
        test.testIfExists();
    }
}

您还声明:

我不知道如何在 Java 中获取 Selenium 的 @Config 部分。

请确保您知道,使用 Conductor 会将您从 Selenium API 中抽象出来。它只是包装了它。 @Config不属于 Selenium,它属于 Conductor。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM