我的xml文件中有两个类,但是当我将它作为测试套件运行时,我无法执行fetch类。我的程序中没有任何错误。我可以运行代码但只执行登录。 感谢您的帮助!!
Xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name="fc.Fetch"/>
<class name="fc.Login"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
主要课程
package fc;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.BeforeSuite;
public class MainClass {
public static WebDriver driver = null;
@BeforeSuite
public void driver() {
if (driver == null) {
System.setProperty("webdriver.chrome.driver", ".//chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.addArguments("start-maximized");
options.addArguments("--disable-notifications");
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://staging-virtual.appspot.com");
}
}
}
登录类代码
package fc;
import org.openqa.selenium.By;
import org.testng.annotations.Test;@Test
public class Login extends MainClass {
By email = By.xpath("//input[@id='email']");
By password = By.xpath("//input[@id='pwd']");
By showpassword = By.xpath("//span[@class='show-pwd']");
By hidepassword = By.xpath("//span[@class='show-pwd']");
By signin = By.xpath("//button[@id='login-btn']");
By gsearch = By.xpath("//a[@class='icon google']");
generateData random = new generateData();
@Test(priority = 1)
public void loginWithBlanks() {
driver.findElement(email).clear();
driver.findElement(password).clear();
driver.findElement(signin).click();
}
@Test(priority = 2)
public void loginWithOnlyPw() {
driver.findElement(email).clear();
driver.findElement(password).clear();
driver.findElement(password).sendKeys(random.generateRandomNames());
driver.findElement(signin).click();
}
@Test(priority = 3)
public void loginWithOnlyUn() {
driver.findElement(email).clear();
driver.findElement(email).sendKeys(random.generateRandomEmail());
driver.findElement(password).clear();
driver.findElement(signin).click();
}
@Test(priority = 4)
public void loginGmailId() {
driver.findElement(email).clear();
driver.findElement(email).sendKeys(random.generateRandomGmailId());
driver.findElement(password).clear();
driver.findElement(password).sendKeys(random.generateRandomAlphanumeric());
driver.findElement(signin).click();
}
@Test(priority = 5)
public void showAndHidePasswordClick() {
driver.findElement(password).click();
driver.findElement(password).clear();
driver.findElement(password).sendKeys(random.generateRandomNames());
driver.findElement(showpassword).click();
driver.findElement(hidepassword).click();
}
@Test(priority = 6)
public void loginFullId() {
driver.findElement(email).clear();
driver.findElement(email).sendKeys("******");
driver.findElement(password).clear();
driver.findElement(password).sendKeys("*********");
driver.findElement(signin).click();
}
@Test(priority = 7)
public void oneStepLogin() {
driver.findElement(email).clear();
driver.findElement(email).sendKeys("@#$%^&*");
driver.findElement(password).clear();
driver.findElement(password).sendKeys("@#$%^&*()");
driver.findElement(signin).click();
}
}
获取类代码
package fc;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
@Test
public class Fetch extends Login {
public Fetch(WebDriver driver) {
this.driver = driver;
}
WebDriverWait wait = new WebDriverWait(MainClass.driver, 10);
By fetch = By.xpath("//div[@class='fetch btn_cst hint--left']");
By accountNumber = By.xpath("//input[@id='accountNumber']");
By load = By.xpath("//a[@class='btn_cst']");
By fetchClose = By.xpath("//span[@class='popupfetchClose']");
By popup = By.xpath("//a[@class='cancel']");
generateData randomData = new generateData();
@Test(priority = 1)
public void fetchButtonClick() {
try {
driver.findElement(fetch).click();
}
catch(Exception e) {
wait.until(ExpectedConditions.visibilityOfElementLocated(fetch));
driver.findElement(fetch).click();
}
}
@Test(priority = 2)
public void fetchAlphabets() {
driver.findElement(accountNumber).sendKeys(randomData.generateRandomNames());
try {
driver.findElement(load).click();
}
catch(Exception e) {
wait.until(ExpectedConditions.visibilityOfElementLocated(load)).click();
}
}
@Test(priority = 3)
public void fetchSplCharacters() {
driver.findElement(accountNumber).clear();
driver.findElement(accountNumber).sendKeys("!@#$%^&*(");
try {
driver.findElement(load).click();
}
catch(Exception e) {
wait.until(ExpectedConditions.visibilityOfElementLocated(load)).click();
}
}
@Test(priority = 4)
public void fetchBlankspace() {
driver.findElement(accountNumber).clear();
try {
driver.findElement(load).click();
}
catch(Exception e) {
wait.until(ExpectedConditions.visibilityOfElementLocated(load)).click();
}
}
@Test(priority = 5)
public void fetchOtherLanguage() {
driver.findElement(accountNumber).clear();
driver.findElement(accountNumber).sendKeys("స్వాగత ");
try {
driver.findElement(load).click();
}
catch(Exception e) {
wait.until(ExpectedConditions.visibilityOfElementLocated(load)).click();
}
}
@Test(priority = 6)
public void fetchWithSplCharacters() {
driver.findElement(accountNumber).clear();
driver.findElement(accountNumber).sendKeys("^&0082509_-#$");
try {
driver.findElement(load).click();
}
catch(Exception e) {
wait.until(ExpectedConditions.visibilityOfElementLocated(load)).click();
}
closePopUp();
}
@Test(priority = 7)
public void fetchSearch() {
driver.findElement(fetch).click();
driver.findElement(accountNumber).click();
driver.findElement(accountNumber).clear();
driver.findElement(accountNumber).sendKeys("95082509");
try {
driver.findElement(load).click();
}
catch(Exception e) {
wait.until(ExpectedConditions.visibilityOfElementLocated(load)).click();
}
closePopUp();
}
@Test(priority = 8)
public void fetchClose() {
fetchButtonClick();
driver.findElement(fetchClose).click();
}
@Test(priority = 9)
public void closePopUp() {
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(popup)).click();
}
catch(Exception e) {
fetchClose();
}
}
}
我已添加所有代码供您阅读
答案 0 :(得分:0)
下面是代码刚刚创建了testNG类文件。 FirstTestNGFile和另一个StackoverflowExamle都会执行这两个类。
FirstTestNGFile Class
public class FirstTestNGFile {
@Test
public void f() {
System.out.println("Test executed");
}
}
StackoverflowExample
public class StackoverflowExamle {
@Test
public void f() {
System.out.println("Test example");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("beforeSuite example");
}
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Test">
<classes>
<class name="firsttestngpackage.StackoverflowExamle"/>
<class name = "firsttestngpackage.FirstTestNGFile"></class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
输出
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
beforeSuite example
Test example
Test executed
===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================