在将应用程序作为TestNG测试运行时破坏测试用例

用户名

我编写了一个Java应用程序,以使用Selenium Webdriver自动执行一些Web应用程序任务。当作为Java应用程序运行时,它们都工作正常。

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

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

我正在使用chromeDriver。我尝试删除主要方法以将其作为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应用程序运行时,不会发生这种情况

兰普拉萨德

您可以显示哪些测试实际上失败了吗?如果您要在执行testng的测试中寻找订单,则默认情况下该订单不是默认的,因此,如果您必须在test1之后运行test2,在test2之后运行test3等,则必须对ex使用优先级(数字越小优先级越高),

@Test(priority=1)
public void Test1() {

}

@Test(priority=2)
public void Test2() {

}

@Test(priority=3)
public void Test3() {

}

希望这可以帮助

不,testng永远不保证默认订购

TestNG依靠反射。当我们使用Java Reflection API对一个类进行内省以找出其中可用的测试方法时,它不保证方法的顺序。因此,永远不能保证独立方法(没有软性或硬性依赖性的方法)执行的顺序。

软依赖性-这通常在TestNG中通过使用@Test批注的优先级属性来实现。之所以称为软依赖性,是因为即使具有更高优先级的先前方法失败,TestNG仍将继续执行所有方法。

硬依赖性-这通常在TestNG中通过对@Test批注使用dependsOnMethods(或)dependsOnGroups属性来实现。之所以称为硬依赖性,是因为只有当上游方法成功运行时,TestNG才会继续执行下游方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

作为Maven测试运行时,TestNG测试失败,但作为TestNG套件运行时,则通过

在 Selenium 中将类作为 TestNG 测试运行时出现 java.lang.StackOverflowError

如何通过Mocha测试运行时参数?

通过Jest获取测试运行时间

将Maven依赖声明为仅测试运行时

Angular:ng测试运行但不执行测试用例

创建Junit测试用例后,“以Junit测试运行”消失

具有预期异常的测试将产生以下错误:“测试运行时,代理进程已停止。”

将QuickTests作为LoadRunner测试运行

如何将脚本作为pytest测试运行

当我取消单元测试运行时,当前运行的测试是否完成?

在 Cucumber 测试运行之前启动一次 Spring Boot 应用程序

如何从卸载应用程序停止Gradle检测的测试运行?

通过Spring Boot集成测试运行原始应用程序

作为插件测试运行时,不同项目中重复的软件包名称会导致InvalidAccessException

我可以在测试运行时跳过JUnit测试吗?

使用SBT测试运行时Spark测试失败

是否可以将测试计划中的测试用例附加到管道发布测试运行结果上?Azure DevOps API V6.0

通过python在运行时运行C程序的测试用例

我想使用空手道将每个.feature文件作为单个TestNG测试运行?

春天开机测试类重用更快的测试运行应用程序上下文?

使用简单的 Express 应用程序完成测试运行后,Jest 测试未退出(一秒)

如何在测试运行时类型检查期间模拟 Python 类?

bazel-测试运行时的可写归档路径

dotnet test命令显示单个测试运行时间?

测试运行时未调用C#中的nUnit SetupFixture类

从测试运行时,getResourceAsStream(“ file”)在哪里搜索?

Junit4在测试运行时引发异常

在硒测试运行时,HTTP URL重定向为HTTPS