我有一个测试,我想知道是否可以一键运行。我的意思是,我有一个班级可以运行其他班级,另一个班级可以运行另一个班级(我现在正在做研究,因此所运行的测试非常基本。所以我有班级
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);
}
}
答案 0 :(得分:1)
您可以通过执行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套件文件来有效地调用多个类。
答案 1 :(得分:0)
<?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="testNGresearch" preserve-order="true">
<classes>
<class name="com.testNGresearch.AllNavigationTestsNG " />
<class name="com.testNGresearch.AllLoginTestsNG" />
</classes>
</test>
<!-- Test -->
</suite> <!-- Suite -->
在指定要测试的类时,请确保您具有正确的程序包名称。
进行测试,例如-登录-将功能完全登录到一个测试中。
注意-像login()
类中的Loginer
一样,这不是一个好方法,因为您需要依靠其他一些功能才能进入此屏幕。您应该尽可能独立地开发测试,并在其内部放入与一个测试有关的断言。
让我知道是否有任何问题。
供参考,
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{
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);
}
@Test(priority = 2)
public void cancelSignUp() {
driver.close();
}
}