gpt4 book ai didi

java - 所有浏览器驱动程序和代码的 Selenium Java 基类是多余的

转载 作者:行者123 更新时间:2023-11-30 06:52:20 25 4
gpt4 key购买 nike

我是 Selenium 和 Java 的新手,我需要基类方面的帮助。我有一个基础,我为驱动程序浏览器及其关闭设置方法。问题是,当我从 main 调用这些方法时,总是会调用 Web 驱动程序并且浏览器会打开很多次。如果我不想重复代码,最佳实践是什么我想要一个良好的项目结构。

主要:

public class Main extends TestBase {
public static void main(String[] args) throws InterruptedException, ClassNotFoundException, SQLException {
LoginTest LoginTest = new LoginTest();
LogofTest LogofTest = new LogofTest();
TestBase TestBase = new TestBase();
LoginTest.setUpBeforeTestMethod();
LoginTest.loginAsAdmin();
LogofTest.logofAsAdmin();
LoginTest.tearDownAfterTestClass();
}
}

测试库:

public class TestBase {
String a = System.setProperty("webdriver.chrome.driver",
"path");
WebDriver driver = new ChromeDriver();

protected WebDriver setUpBeforeTestClass() {

return driver;
}

protected void setUpBeforeTestMethod() {
driver.get("website");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

protected void tearDownAfterTestClass() {
driver.close();
}}

登录测试:

public class LoginTest extends TestBase {

public void login() throws InterruptedException {
WebElement username = driver.findElement(By.name("username"));
username.sendKeys("username");
}
}

主要是不想再写了

WebDriver driver = new ChromeDriver();
driver.get("website"); System.setProperty("webdriver.chrome,"path");

对于函数或类中的每个测试。所以我想创建基类并继承它。

最佳答案

示例Selenium使用页面对象模型使用JUnit进行测试

测试库

public class TestBase
{
private String a = System.setProperty("webdriver.chrome.driver", "path");
protected WebDriver driver;

@Before //Before each test case, use BeforeClass for before each test class
public static void setUpBeforeTestCase() {
driver = new ChromeDriver();
driver.get("website");
}

@After
public static void tearDownAfterTestCase() {
driver.Quit(); //driver.Close() closes the window, but doesn't properly dispose of the driver
}
}

登录测试:

public class LoginTest extends TestBase {

@Test
public void loginAndOutAsAdmin(){
LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);
LandingPage landingPage = loginPage.login("adminUser", "adminPassword");
landingPage.logout();
//Do some sort of assert here that you are logged out
}
}

基本页面

public class BasePage
{
protected WebDriver driver;

//Other common stuff your Page Objects will do, like wait for an element
}

登录页面

public class LoginPage extends BasePage
{
@FindBy(how = How.NAME, using = "username")
private WebElement usernameBox;

//something for passwordBox and loginButton

public LoginPage(WebDriver currentDriver)
{
driver = currentDriver;
}

public LandingPage login(String username, String password)
{
usernameBox.sendKeys(username);
passwordBox.sendKeys(password);
loginButton.click();

return PageFactory.initElements(driver, LandingPage.class);
}
}

我还没有尝试编译这个,但这是基本的想法。我会让您填写详细信息。

关于java - 所有浏览器驱动程序和代码的 Selenium Java 基类是多余的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42513068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com