Я написал Java-приложение для использования селенового веб-драйвера для автоматизации нескольких задач веб-приложения.Все они работали нормально, когда работали как приложение Java.
Чтобы использовать функцию отчетов TestNG, я запустил приложение как тест TestNG вместо приложения JAVA.Тот же тест, который работал как приложение JAVA, не работает, когда я запускаю как testNG.
Однако, я предполагаю, что правильно настроил TestNG, поскольку первый тестовый сценарий, который используется для входа в веб-приложение, проходит(метод 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