当 运行 应用程序作为 TestNG 测试时测试用例中断
Test Case breaking when running the application as a TestNG test
我写了一个 java 应用程序来使用 selenium webdriver 来自动执行一些 web 应用程序任务。当 运行 作为 Java 应用程序时,它们都工作正常。
为了使用 TestNG 报告功能,我 运行 应用程序作为 TestNG 测试而不是 JAVA 应用程序。当我 运行 作为 testNG.
时,与 JAVA 应用程序相同的测试失败了
但是,我猜我已经正确设置了 TestNG,因为用于登录到 webapp 的第一个测试用例正在通过(下面代码中的 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 等等,那么您必须使用优先级(数字越低优先级越高) 例如,
@Test(priority=1)
public void Test1() {
}
@Test(priority=2)
public void Test2() {
}
@Test(priority=3)
public void Test3() {
}
希望这对您有所帮助
不,testng 从不 gua运行tees 默认排序
TestNG 依赖反射。当我们使用 Java 反射 API 来反省 class 以找出其中可用的测试方法时,运行 不保证方法顺序。因此,独立方法(既没有软依赖也没有硬依赖的方法)的执行顺序永远不会被 gua运行teed.
软依赖 - 这通常在 TestNG 中通过使用 @Test 注释的优先级属性来实现。它被称为软依赖,因为 TestNG 将继续执行所有方法,即使先前具有更高优先级的方法失败。
硬依赖 - 这通常在 TestNG 中通过使用 @Test 注释的 dependsOnMethods(或)dependsOnGroups 属性来实现。之所以称为硬依赖,是因为当且仅当上游方法 运行 成功时,TestNG 才会继续执行下游方法。
默认情况下,testng 按照方法名称的字母顺序执行方法。通常你不会使用 main 方法来测试。注释和优先级用于设置执行顺序
Testng 框架将运行 测试方法按字母顺序排列。我可以看到你的测试方法是依赖的,它应该按顺序排列。您可以按照您希望的方式将测试方法的优先级设置为 运行。
您可以参考下面link设置优先级。
我写了一个 java 应用程序来使用 selenium webdriver 来自动执行一些 web 应用程序任务。当 运行 作为 Java 应用程序时,它们都工作正常。
为了使用 TestNG 报告功能,我 运行 应用程序作为 TestNG 测试而不是 JAVA 应用程序。当我 运行 作为 testNG.
时,与 JAVA 应用程序相同的测试失败了但是,我猜我已经正确设置了 TestNG,因为用于登录到 webapp 的第一个测试用例正在通过(下面代码中的 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 等等,那么您必须使用优先级(数字越低优先级越高) 例如,
@Test(priority=1)
public void Test1() {
}
@Test(priority=2)
public void Test2() {
}
@Test(priority=3)
public void Test3() {
}
希望这对您有所帮助
不,testng 从不 gua运行tees 默认排序
TestNG 依赖反射。当我们使用 Java 反射 API 来反省 class 以找出其中可用的测试方法时,运行 不保证方法顺序。因此,独立方法(既没有软依赖也没有硬依赖的方法)的执行顺序永远不会被 gua运行teed.
软依赖 - 这通常在 TestNG 中通过使用 @Test 注释的优先级属性来实现。它被称为软依赖,因为 TestNG 将继续执行所有方法,即使先前具有更高优先级的方法失败。
硬依赖 - 这通常在 TestNG 中通过使用 @Test 注释的 dependsOnMethods(或)dependsOnGroups 属性来实现。之所以称为硬依赖,是因为当且仅当上游方法 运行 成功时,TestNG 才会继续执行下游方法。
默认情况下,testng 按照方法名称的字母顺序执行方法。通常你不会使用 main 方法来测试。注释和优先级用于设置执行顺序
Testng 框架将运行 测试方法按字母顺序排列。我可以看到你的测试方法是依赖的,它应该按顺序排列。您可以按照您希望的方式将测试方法的优先级设置为 运行。
您可以参考下面link设置优先级。