首页 > 解决方案 > 将应用程序作为 TestNG 测试运行时测试用例中断

问题描述

我编写了一个 java 应用程序来使用 selenium webdriver 来自动化一些 web 应用程序任务。当作为 Java 应用程序运行时,它们都运行良好。

为了使用 TestNG 报告功能,我将应用程序作为 TestNG 测试而不是 JAVA 应用程序运行。当我作为 testNG 运行时,与 JAVA 应用程序相同的测试失败。

但是,我猜我已经正确设置了 TestNG,因为用于登录 webapp 的第一个测试用例正在通过(下面代码中的 webLogin 方法)

我正在使用 chromeDriver。我尝试删除 main 方法以将其作为 testNG 应用程序运行。但它没有用。我确保我使用的元素路径定位器在使用 testNG 时仍然有效。

package myPackage;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
import com.google.common.base.Function;

public class checkNavigation {
    public static WebDriver driver;
    public static WebDriverWait wait;
    public static void main(String args[]) throws InterruptedException{

    Configure();        

    wait = new WebDriverWait(driver, 30);

    webLogin(); 
    addClient();
    addGoal();
    addInsurance();
    validateInputs();
    endSession();
}

@BeforeTest
public static void Configure() {
    System.setProperty("webdriver.chrome.driver", "/Users/divyakapa/Desktop/automation/chromedriver");

    driver = new ChromeDriver();

    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();
    driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);

    driver.get("https://example.com");  
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}

@Test
public static void webLogin() {
    getElement(By.xpath("//*[@id=\"id\"]")).sendKeys("admin");
    getElement(By.xpath("//*[@id=\"pw\"]")).sendKeys("password");       
    getElement(By.xpath("//*[@id=\"ember383\"]/div/div/form/button/span")).click();
}

@Test
public static void addClient() {        
    getElement(By.xpath("//*[@id=\"ember744\"]/button/div")).click();
    getElement(By.xpath("//*[@id=\"ember744\"]/div/button[1]/div[2]/div")).click();

    getElement(By.xpath("//*[@id=\"newInputFirst\"]")).sendKeys("firstName");
    getElement(By.xpath("//*[@id=\"newInputLast\"]")).sendKeys("lastName");
    getElement(By.xpath("//*[@id=\"newPersonInputBirthday\"]")).sendKeys("1991");

    Select location = new Select(driver.findElement(By.xpath("//*[@id=\"newUserInputProvince\"]")));
    location.selectByVisibleText("Place1");

    Select isRetired = new Select(driver.findElement(By.xpath("//*[@id=\"alreadyRetiredDropdown\"]")));
    isRetired.selectByVisibleText("No");

    Select age = new Select(driver.findElement(By.xpath("//*[@id=\"newRetirementAge\"]")));
    age.selectByVisibleText("60");

    getElement(By.xpath("//*[@id=\"data-entry-modal\"]/div[2]/div/div[1]/div[2]/button[2]")).click();
}

@Test
public static void addGoal() {
    getElement(By.xpath("//*[@id=\"ember2328\"]/button/div")).click();

    getElement(By.xpath("//*[@id=\"ember2328\"]/div/div[1]/div[2]/button[3]")).click();
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"ember2464\"]/ul/li[1]/div/div/div[2]/div/span"))).click();

    getElement(By.xpath("//*[@id=\"basicExpenseInputAmount\"]")).clear();
    getElement(By.xpath("//*[@id=\"basicExpenseInputAmount\"]")).sendKeys("90000");
    getElement(By.xpath("//*[@id=\"ember2563\"]/div/div[2]/div[2]/button[2]")).click();

    // Add income
    getElement(By.xpath("//*[@class=\"add-button \"]")).click();
    getElement(By.xpath("//*[@data-test-model-type=\"income\"]")).click();

    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@class=\"list-group\"]/li[1]"))).click();

    getElement(By.xpath("//*[@id=\"employmentInputName\"]")).clear();
    getElement(By.xpath("//*[@id=\"employmentInputName\"]")).sendKeys("Company");

    getElement(By.xpath("//*[@id=\"employmentInputSalary\"]")).sendKeys("95000");
    getElement(By.xpath("//*[@id=\"employmentInputBonus\"]")).sendKeys("5000");
    getElement(By.xpath("//*[@id=\"employmentInputBenefitsInKind\"]")).sendKeys("1000");

    getElement(By.xpath("//*[@aria-label=\"Save\"]")).click();

}

@Test
public static void addInsurance() {
    getElement(By.xpath("//*[@class=\"add-button \"]")).click();        
    getElement(By.xpath("//*[@data-test-model-type=\"protection\"]")).click();

    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@class=\"list-group\"]/li[1]"))).click();

    getElement(By.xpath("//*[@id=\"termLifeName\"]")).clear();
    getElement(By.xpath("//*[@id=\"termLifeName\"]")).sendKeys("BlueCrossBlueShield");
    getElement(By.xpath("//*[@id=\"ukTermProtectionSalaryMultiplier\"]")).clear();
    getElement(By.xpath("//*[@id=\"ukTermProtectionSalaryMultiplier\"]")).sendKeys("5");

    Select empId = new Select(driver.findElement(By.xpath("//*[@id=\"termLifeInsuranceEmploymentId\"]")));
    empId.selectByVisibleText("Company");

    getElement(By.xpath("//*[@aria-label=\"Save\"]")).click();
}

@Test
public static void validateInputs() {
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    if(!(driver.findElements(By.xpath("//*[@data-test-accordion-header=\"goals\"]")).size() > 0)) throw new NullPointerException("Income Failed to ADD");
    if(!(driver.findElements(By.xpath("//*[@data-test-accordion-header=\"income\"]")).size() > 0)) throw new NullPointerException("Income Failed to ADD");
    if(!(driver.findElements(By.xpath("//*[@data-test-accordion-header=\"protection\"]")).size() > 0)) throw new NullPointerException("Income Failed to ADD");
}

public static WebElement getElement(final By locator) {
    FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver).ignoring(NoSuchElementException.class);

    WebElement element = wait.until(new Function<WebDriver, WebElement>() {

        @Override
        public WebElement apply(WebDriver arg0) {
            return arg0.findElement(locator);
        }

    });

        return element;
    }

     @AfterTest
    public static void endSession() {
    driver.close();
    driver.quit();
    }
}

运行上面的代码,得到以下错误:

Default suite
Total tests run: 5, Failures: 4, Skips: 0

我还看到,即使测试通过了,登录页面也需要很长时间(大约 10 秒)。当我将代码作为 Java 应用程序运行时,不会发生这种情况

标签: selenium-webdrivertestngselenium-chromedriver

解决方案


Testng 框架将按字母顺序运行测试方法。我可以看到您的测试方法是依赖的,它应该按顺序排列。您可以按照您希望它运行的方式设置测试方法的优先级。

您可以参考以下链接来设置优先级。

TestNG 优先级设置


推荐阅读