[[oktatas:web:selenium|< Selenium]] ====== Selenium - Java==== * **Szerző:** Sallai András * Copyright (c) Sallai András, 2023 * Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC Attribution-Share Alike 4.0 International]] * Web: https://szit.hu ===== Szükséges ===== Szoftver: * **VSCode** Programozói könyvtár: * slf4j-api-x.y.z.jar - https://www.slf4j.org/ * reactive-streams.x.y-z.jar - https://www.reactive-streams.org/ * selenium-java-x.y.z.zip Az slf4j és a Reactive-Streams letölthető a következő helyről: * https://mvnrepository.com/ ==== Selenium ==== A Selenium a következő helyről tölthető le: * https://www.selenium.dev/downloads/ Keressük a Java ikont. Kapunk egy .zip fájlt. Csomagoljuk ki. ==== TestNG ==== * https://testng.org/ ==== Demo webhely ==== * https://demo.automationtesting.in/Register.html (2023) ===== Projekt készítése ===== A selenium-java-x.y.z.zip fájl tartalmát egyben másoljuk a Java projekt lib könyvtárába, lib könyvtárastól. A Maven tárolóból letöltött másik két .jar fájlt is másoljuk ide. app01/ |-.vscode/ | `-setting.json |-bin/ |-lib/ |-src/ | `-App.java `-README.md import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class App { public static void main(String[] args) throws Exception { WebDriver driver = new ChromeDriver(); driver.get("https://szit.hu"); System.out.println(driver.getTitle()); driver.quit(); } } ===== A szit.hu kereső indítása ===== import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class App { public static void main(String[] args) throws Exception { WebDriver driver = new ChromeDriver(); driver.get("https://szit.hu"); WebElement elem = driver.findElement(By.name("q")); elem.sendKeys("recept" + Keys.ENTER); Thread.sleep(5000); driver.quit(); } } ===== TestNG futtatás ===== Másoljuk a lib könyvtárba a testng-x.y.z.jar fájlt is. Test nevű könyvtár: * A teszteknek egy **test** nevű könyvtárat készítünk. * VSCode-ban a **test** könyvtár felett kattintsunk jobb egér gombbal, majd: * **Add Folder to Java Source Path** app01/ |-.vscode/ | `-setting.json |-bin/ |-lib/ |-src/ |-test/ | `-TestSzit.java `-README.md A tesztben használjuk Test annotációt és az Assert osztályt. import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.Test; public class TestSzit { @Test public void testTitle() { WebDriver driver = new ChromeDriver(); driver.get("https://szit.hu"); WebElement elem = driver.findElement(By.name("q")); elem.sendKeys("recept" + Keys.ENTER); boolean actual = driver.getPageSource().contains("Gluténmentes"); Assert.assertTrue(actual); driver.quit(); } } ===== Több teszt ===== Az AfterTest, BeforeTest, BeforeMethod annotációkat használjuk a tesztek előkészítésére. import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class TestSzit { WebDriver driver; @BeforeTest public void initTest() { driver = new ChromeDriver(); } @BeforeMethod public void initMethods() { driver.get("https://szit.hu"); } @Test public void testRecept() { WebElement elem = driver.findElement(By.name("q")); elem.sendKeys("recept" + Keys.ENTER); boolean actual = driver.getPageSource().contains("Gluténmentes"); Assert.assertTrue(actual); } @Test public void testAngularContent() { WebElement elem = driver.findElement(By.name("q")); elem.sendKeys("Angular" + Keys.ENTER); boolean actual = driver.getPageSource().contains("Angular"); Assert.assertTrue(actual); } @AfterTest public void endTest() { driver.quit(); } } * @BeforeTest - Az összes teszt előtt hajtódik végre. * @AfterTest - Az összes teszt után hajtódik végre. * @BeforeMethod - Az összes metódus előtt végrehajtódik. * @AfterMethod - Az összes metódus után végrehajtódik. A tesztek végrehajtási sorrendjét a megadásuk sorrendje nem befolyásolja, csak az annotáció határozza meg a sorrendet. A példában @AfterTest annotáció nincs. A példa teszt megnyitja a szit.hu weboldalt, majd beírja a "recept" szót, majd megnyomja az "Enter" billentyűt. Megnézi, hogy a a találati weboldalon szerepel-e a "Gluténmentes" szót. A következő teszt megint a szit.hu főoldalára navigál, majd beírja az Angular szót, megnyomja az "Enter" billentyűt, majd megnézi, hogy szerepel a találati weboldalon az Angular szó. Végül a teszt bezárja a böngészőt. ===== TestNG prioritás ===== Beállítható, hogy hamarabb hajtódjon végre egy teszt. @Test(priority = 0) //... @Test(priority = 7) //... ===== Linkek ===== * https://www.guru99.com/all-about-testng-and-selenium.html (2023)