我编写了一个java应用程序来使用selenium webdriver来自动化一些web应用程序任务。当作为Java应用程序运行时,它们都工作得很好。
为了使用JAVA特性,我将应用程序作为TestNG测试而不是TestNG应用程序运行。当我作为testNG运行时,同样的测试作为JAVA应用程序也失败了。
但是,我猜我已经正确设置了TestNG,因为第一个用于登录the应用程序的测试用例通过了(下面代码中的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应用程序运行时,不会发生这种情况
发布于 2019-05-10 11:21:23
你能指出哪些测试实际上是失败的吗?如果你在testng测试执行中寻找顺序,它不是默认的,所以如果你必须在test1之后运行test2,在test2之后运行test3等,那么你必须对ex使用priority(优先级越高,数值越小)。
@Test(priority=1)
public void Test1() {
}
@Test(priority=2)
public void Test2() {
}
@Test(priority=3)
public void Test3() {
}希望这能有所帮助
不,默认情况下,testng从不保证排序
TestNG依赖于反射。当我们使用Java反射API对一个类进行内省以找出其中可用的测试方法时,它并不保证方法的顺序。因此,独立方法(既不具有软依赖又不具有硬依赖的方法)的执行顺序永远不会得到保证。
软依赖-在TestNG中,这通常是通过使用@Test注释的优先级属性来实现的。它被称为软依赖,因为即使之前优先级更高的方法失败了,TestNG也会继续执行所有的方法。
硬依赖-这通常是在TestNG中通过对@Test注释使用dependsOnMethods (或) dependsOnGroups属性来实现的。之所以称之为硬依赖,是因为当且仅当上游方法成功运行时,TestNG才会继续执行下游方法。
发布于 2019-05-10 13:43:16
默认情况下,testng按照方法名的字母顺序执行方法。通常,您不会使用main方法来测试be。注释和优先级用于设置执行的顺序
发布于 2019-05-11 20:22:57
Testng框架将按字母顺序运行测试方法。我可以看出你的测试方法是依赖的,它应该是按顺序的。您可以按照您希望的方式设置测试方法的优先级。
您可以参考下面的链接来设置优先级。
https://stackoverflow.com/questions/56069796
复制相似问题