Перед написанием первого теста в Eclipse с использованием Selenium Webdriver просмотрите предыдущую статью о создании проекта и добавлении jar-файлов.
Для автоматизации будет использоваться демо-сайт, размещенный на отдельном хостинге. Будут автоматизированы следующие шаги:
1. Открытие страницы сайта в Firefox.
2. Вход на сайт с введением пары – логин/пароль.
3. Выход.
1) Скопируйте и вставьте следующий код в созданный ранее класс FirstTestCase.
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 |
package automationFramework; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class FirstTestCase { private static WebDriver driver = null; public static void main(String[] args) { // 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); driver.get("http://autoqa.pp.ua/wp-admin"); // 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."); } // Close the driver driver.quit(); } } |
2) Чтобы запустить тест выполните команду Run > Run As > Java Application или правым кликом в коде с выбором команды Run As > Java Application.
3) После нескольких секунд откроется браузер Mozilla и Вы увидите как Selenium открывает сайт, выполняет вход, проверяет вход, выходит и потом закрывает браузер.
Как только выполнение скрипта заканчивается, Вы увидите сообщение в консоли.