【问题标题】:Unable to execute second and third classes in xml file -TestNG无法在 xml 文件中执行第二和第三类 -TestNG
【发布时间】:2018-06-24 01:59:20
【问题描述】:

我的 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();
        }
    }
}

我已经添加了所有代码供您阅读

【问题讨论】:

  • 您确定在 fetch 类上添加了@Test 注释吗?
  • 是的,我已经添加了@Test 注释,但它仍然没有被执行
  • 发布课程代码
  • @MelvinXavier Fetch 扩展 Logn 有什么要求?
  • 利用驱动功能@Grasshopper

标签: selenium testing testng test-suite


【解决方案1】:

以下是刚刚创建的 testNG 类文件的代码。 FirstTestNGFile 和另一个 StackoverflowExamle 两个类都被执行。

FirstTestNGFile 类

    public class FirstTestNGFile {


      @Test
      public void f() {

      System.out.println("Test executed");


  }
}

堆栈溢出示例

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
===============================================

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多