gpt4 book ai didi

java - 如何运行一个运行多个 Selenium 类的类?

转载 作者:行者123 更新时间:2023-11-28 21:21:11 25 4
gpt4 key购买 nike

我有一个测试,我想知道是否可以一键运行。我的意思是我有一个运行其他类的类和另一个运行一个类的类(我现在正在做研究,所以运行的测试非常基础。所以我有类

package testNGresearch;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import multipleTest.Loginer;

public class AllLoginTestsNG extends OneClickTest{

public static WebDriver driver;
String baseUrl = "https://9gag.com/";
@BeforeTest
public void openBrowser() {
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}

@Test(priority = 1)
public void GetUrl() throws InterruptedException {
driver.get(baseUrl);
Thread.sleep(1500);
Loginer.login(driver);
}
@Test(priority = 2)
public void clickSignUp() {
Loginer1.signUp(driver);
}
@Test(priority = 3)
public void cancelSignUp() {
Loginer2.cancelSignUp(driver);
driver.close();
}
@Test(priority=4)
public void navigationTests() throws InterruptedException {
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(baseUrl);
Thread.sleep(1500);
NavigationTestNG.navigation(driver);
}
}

然后是另一个(单独的文件)

package testNGresearch;

import org.openqa.selenium.By;
import org.testng.annotations.Test;

public class Loginer extends AllLoginTestsNG{


@Test
public void login() {
driver.findElement(By.xpath("/html/body/div[7]/div[1]/div[2]/div/div[3]/button[2]/span")).click();
driver.findElement(By.xpath("//*[@id=\"jsid-login-button\"]")).click();
driver.findElement(By.xpath("//*[@id=\"login-email-name\"]")).sendKeys("someuser");
driver.findElement(By.xpath("//*[@id=\"login-email-password\"]")).sendKeys("somepass");
driver.findElement(By.xpath("//*[@id=\"login-email\"]/div[3]/input")).click();
}
}

然后是 AllLogin 类运行的第二个

package testNGresearch;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class Loginer1 extends AllLoginTestsNG {

@Test
public static void signUp(WebDriver driver) {
driver.findElement(By.xpath("//*[@id=\"jsid-signup-button\"]")).click();
}

}

现在我有一个类 AllNavigation

package testNGresearch;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class AllNavigationTestsNG extends AllLoginTestsNG {

public static WebDriver driver;
String baseUrl = "https://9gag.com/";
@BeforeTest
public void openBrowser() {
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test(priority = 1)
public void startNavigating (WebDriver driver) throws InterruptedException {
driver.get(baseUrl);
Thread.sleep(1500);
NavigationTestNG.navigation(driver);
}
}

运行

package testNGresearch;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class NavigationTestNG extends AllNavigationTestsNG {

@Test (priority = 1)
public static void navigation(WebDriver driver) throws InterruptedException {
Thread.sleep(500);
driver.findElement(By.xpath("/html/body/div[7]/div[1]/div[2]/div/div[3]/button[2]/span")).click();
Thread.sleep(500);
driver.findElement(By.xpath("//*[@id=\"top-nav\"]/div/a")).click();
}
}

所以问题是 - 是否可以创建一个运行 AllLogin 类和 AllNavigationTestNG 类的类(这使得它们一个接一个地运行)我创建了一个抛出错误的 OneClickTest 类

package testNGresearch;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class OneClickTest {

public static void main(String[] args) {

WebDriver driver;
AllLoginTestsNG.allLogin(driver);
AllNavigationTestsNG.anotherLogin(driver);
}
}

最佳答案

您可以通过执行 TestNG.XML 套件文件来实现这一点,

这是示例代码引用:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" verbose="1">
<test name="My Account Test" preserve-order="true">
<classes>
<class name="myAccount.login" />
<class name="myAccount.registration" />
<class name="myAccount.forgotPassword" />
<class name="myAccount.signOut" />
</classes>
</test>
<!-- Test -->
</suite> <!-- Suite -->

此处登录脚本将首先调用,然后按定义的顺序调用注册等。通过这种方式,您可以通过单击 TestNG.XML 套件文件来有效地调用多个类。

关于java - 如何运行一个运行多个 Selenium 类的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51134535/

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