В первой статье мы установили TestNG. Теперь приступим к созданию класса.
Проделайте следующие шаги:
1) В контекстном меню созданного пакета выберите TestNG и выберите “TestNG Class”.
2) Назовите класс “TestNG”. Выберите следующие аннотации – “@BeforeMethod“, “@AfterMethod” и нажмите Finish.
3) Новосозданный класс должен выглядить следующим образом.
Время написать первый тест-кейс с помощью TestNG.
5) Воспользуемся тест-кейсом и разделим его на три части.
@BeforeMethod : Запуск Firefox и переход по URL
@Test : Ввод имени пользователя и пароля, отображение информации в консоле и Log out
@AfterMethod : Закрытие браузера
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
package automationFramework; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; public class TestNG { public WebDriver driver; @Test public void main() { // Find the element that's ID attribute is 'user_login' (Username) // Enter Username on the element found by above desc. driver.findElement(By.id("user_login")).sendKeys("subscriber"); // Find the element that's ID attribute is 'user_pass' (Password) // Enter Password on the element found by the above desc. driver.findElement(By.id("user_pass")).sendKeys("2016subscriberpasssword2016ok"); // Now submit the form. WebDriver will find the form for us from the element driver.findElement(By.id("wp-submit")).click(); if (driver.findElement(By.id("profile-page")).isDisplayed()) { // Print a Log In message to the screen System.out.println("Login successfully."); } } @BeforeMethod public void beforeMethod() { // Create a new instance of the Firefox driver driver = new FirefoxDriver(); //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //Launch the Wordpress Admin site driver.get("http://autoqa.pp.ua/wp-admin"); } @AfterMethod public void afterMethod() { // Close the driver driver.quit(); } } |
6) Запустите тест-кейс с помощью команды Run As -> TestNG Test.
Результаты запуска Testng тест-кейса
7) После завершения выполнения тест-кейса перейдите в окно результата запуска TestNg Result.
Он отображает “passed : 1″. Это означает, что тест-кейс выполнилcя успешно и Passed.
Еще есть три вкладки: “All Tests”, “Failed Tests” и “Summary”. Нажмите “All Tests”.
Здесь присутствует информация о том, какой метод запускался и время выполнения.
8) TestNG также генерирует HTML отчет. Чтобы его просмотреть, перейдите в директорию проекта и в папку test-output.
9) Откройте “emailable-report.html” в браузере.
10) TestNG также создает “index.html”, размещенный в папке test-output. Этот репорт позволяет перейти по ссылкам Groups и Reporter Output.