diff --git a/README.md b/README.md new file mode 100644 index 0000000..53fbc9a --- /dev/null +++ b/README.md @@ -0,0 +1,46 @@ +# Start Using WebDriver Examples + +This is the mini-sized package to start use WebDriver with Firefox and Chrome + +## Pre-requisite: +1. MacOS or Linux (Very important, not supporting any other OS, like Windows). +2. Java +3. Maven +4. Firefox Installed +5. Chrome Installed + +## General Environment installation +1. Install Homebrew from http://www.brew.sh +2. Install Java +``` +brew cask install java +``` +3. Install Maven +``` +brew install maven +``` + +## Expected Automate Test Result: + +1. Open browser, e.g., Firefox + +2. Visit "https://www.duckduckgo.com" + +3. Verify if the title is "Hawaiian pizza" + +4. Quit the driver + +## Support + +This tutorial is released into the public domain by ITNove under WTFPL. + +[![WTFPL](http://www.wtfpl.net/wp-content/uploads/2012/12/wtfpl-badge-1.png)](http://www.wtfpl.net/) + +This README file was originally written by [Guillem Hernández Sola](https://www.linkedin.com/in/guillemhernandezsola/) and is likewise released into the public domain. + +Please contact ITNove for further details. + +* ITNOVE a Cynertia Consulting +* Passeig de Gràcia 110, 4rt 2a +* 08008 Barcelona +* T: 93 184 53 44 \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..523a4c3 --- /dev/null +++ b/pom.xml @@ -0,0 +1,49 @@ + + 4.0.0 + + com.itnove.trainings.webdriver + StartUsingWebDriver + 17.09.12 + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.6 + 1.6 + + + + + jar + JunitMaven + http://maven.apache.org + + UTF-8 + + + + junit + junit + 4.12 + + + org.seleniumhq.selenium + selenium-java + 3.5.3 + + + org.seleniumhq.selenium + selenium-firefox-driver + 3.5.3 + + + org.seleniumhq.selenium + selenium-chrome-driver + 3.5.3 + + + diff --git a/src/main/java/com/itnove/trainings/junit/startUsingWebDriver/App.java b/src/main/java/com/itnove/trainings/junit/startUsingWebDriver/App.java new file mode 100644 index 0000000..4fc06ec --- /dev/null +++ b/src/main/java/com/itnove/trainings/junit/startUsingWebDriver/App.java @@ -0,0 +1,13 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +/** + * Hello world! + * + */ +public class App { + + public String getWish() { + return "Hello"; + } + +} diff --git a/src/main/resources/2-logo-B_activa.png b/src/main/resources/2-logo-B_activa.png new file mode 100644 index 0000000..d517495 Binary files /dev/null and b/src/main/resources/2-logo-B_activa.png differ diff --git a/src/main/resources/chromedriver-linux b/src/main/resources/chromedriver-linux new file mode 100755 index 0000000..adba49a Binary files /dev/null and b/src/main/resources/chromedriver-linux differ diff --git a/src/main/resources/chromedriver-macos b/src/main/resources/chromedriver-macos new file mode 100755 index 0000000..5428aa4 Binary files /dev/null and b/src/main/resources/chromedriver-macos differ diff --git a/src/main/resources/geckodriver-linux b/src/main/resources/geckodriver-linux new file mode 100755 index 0000000..b8d586b Binary files /dev/null and b/src/main/resources/geckodriver-linux differ diff --git a/src/main/resources/geckodriver-macos b/src/main/resources/geckodriver-macos new file mode 100755 index 0000000..bbcf216 Binary files /dev/null and b/src/main/resources/geckodriver-macos differ diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/AlertsTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/AlertsTest.java new file mode 100644 index 0000000..de0aad6 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/AlertsTest.java @@ -0,0 +1,71 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + + + +import org.junit.Test; +import org.openqa.selenium.Alert; +import org.openqa.selenium.By; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + + +/** + * Unit test for simple App. + */ +public class AlertsTest extends BaseTest { + + public void acceptAlert() { + try { + WebDriverWait wait = new WebDriverWait(driver, 2); + wait.until(ExpectedConditions.alertIsPresent()); + Alert alert = driver.switchTo().alert(); + alert.accept(); + } catch (Exception e) { + //exception handling + } + } + + public void dismissAlert() { + try { + WebDriverWait wait = new WebDriverWait(driver, 2); + wait.until(ExpectedConditions.alertIsPresent()); + Alert alert = driver.switchTo().alert(); + alert.dismiss(); + } catch (Exception e) { + //exception handling + } + } + public void promptAlert() { + try { + WebDriverWait wait = new WebDriverWait(driver, 2); + wait.until(ExpectedConditions.alertIsPresent()); + Alert alert = driver.switchTo().alert(); + alert.sendKeys("Hola hola super hola"); + alert.accept(); + } catch (Exception e) { + //exception handling + } + } + + + @Test + public void testApp() throws InterruptedException { + driver.navigate().to("https://the-internet.herokuapp.com/javascript_alerts"); + driver.findElement(By.xpath(".//*[@id='content']/div/ul/li[1]/button")).click(); + Thread.sleep(2000); + acceptAlert(); + Thread.sleep(2000); + driver.findElement(By.xpath(".//*[@id='content']/div/ul/li[2]/button")).click(); + Thread.sleep(2000); + acceptAlert(); + Thread.sleep(2000); + driver.findElement(By.xpath(".//*[@id='content']/div/ul/li[2]/button")).click(); + Thread.sleep(2000); + dismissAlert(); + Thread.sleep(2000); + driver.findElement(By.xpath(".//*[@id='content']/div/ul/li[3]/button")).click(); + Thread.sleep(2000); + promptAlert(); + Thread.sleep(2000); + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/AppTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/AppTest.java new file mode 100644 index 0000000..e9fdc53 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/AppTest.java @@ -0,0 +1,26 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.ResultsPage; +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.SearchPage; +import org.junit.Assert; +import org.junit.Test; + + +/** + * Unit test for simple App. + */ +public class AppTest extends BaseTest { + + @Test + public void testApp() throws InterruptedException { + driver.navigate().to("http://www.duckduckgo.com"); + SearchPage searchPage = new SearchPage(driver); + searchPage.searchKeyword("Hawaiian pizza"); + Thread.sleep(2000); + ResultsPage resultsPage = new ResultsPage(driver); + Assert.assertTrue(resultsPage.isResultsListPresent()); + resultsPage.clickOnFirstResult(); + Thread.sleep(2000); + Assert.assertTrue(!driver.getCurrentUrl().contains("duckduckgo")); + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/BaseTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/BaseTest.java new file mode 100644 index 0000000..c4dc57b --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/BaseTest.java @@ -0,0 +1,53 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import org.junit.After; +import org.junit.Before; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.interactions.Actions; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.RemoteWebDriver; +import org.openqa.selenium.support.ui.WebDriverWait; + +import java.io.File; +import java.io.IOException; +import java.util.concurrent.TimeUnit; + + +/** + * Created by guillem on 29/02/16. + */ +public class BaseTest { + public RemoteWebDriver driver; + public Actions hover; + public static long timeOut = 120; + public static LocalRemoteWebDriverWait wait; + public static JavascriptExecutor jse; + + @Before + public void setUp() throws IOException { + DesiredCapabilities capabilities = DesiredCapabilities.chrome(); + System.setProperty("webdriver.chrome.driver", "src" + File.separator + "main" + File.separator + "resources" + File.separator + "chromedriver-linux"); + driver = new ChromeDriver(capabilities); + //DesiredCapabilities capabilities = DesiredCapabilities.firefox(); + //System.setProperty("webdriver.gecko.driver", + // "src" + File.separator + "main" + // + File.separator + "resources" + // + File.separator + "geckodriver-linux"); + // driver = new FirefoxDriver(capabilities); + wait = new LocalRemoteWebDriverWait(driver, timeOut); + hover = new Actions(driver); + driver.manage().deleteAllCookies(); + driver.manage().timeouts().pageLoadTimeout(timeOut, TimeUnit.SECONDS); + driver.manage().timeouts().setScriptTimeout(timeOut, TimeUnit.SECONDS); + driver.manage().timeouts().implicitlyWait(timeOut, TimeUnit.SECONDS); + } + + @After + public void tearDown() { + driver.quit(); + } + +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/BasicAuth.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/BasicAuth.java new file mode 100644 index 0000000..36990e5 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/BasicAuth.java @@ -0,0 +1,20 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.ResultsPage; +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.SearchPage; +import org.junit.Assert; +import org.junit.Test; + + +/** + * Unit test for simple App. + */ +public class BasicAuth extends BaseTest { + + @Test + public void testApp() throws InterruptedException { + driver.navigate().to("https://admin:admin@the-internet.herokuapp.com/basic_auth"); + Thread.sleep(5000); + + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/ChallengingDomTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/ChallengingDomTest.java new file mode 100644 index 0000000..3d4bf0e --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/ChallengingDomTest.java @@ -0,0 +1,31 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.ResultsPage; +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.SearchPage; +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import java.util.List; + + +/** + * Unit test for simple App. + */ +public class ChallengingDomTest extends BaseTest { + + @Test + public void testApp() throws InterruptedException { + driver.navigate().to("https://the-internet.herokuapp.com/challenging_dom"); + List botons = + driver.findElements( + By.xpath(".//div[@class='example']/div/div/div[1]/a")); + for (int i = 0; i < botons.size(); i++) { + WebElement botonDeTurno = + driver.findElement( + By.xpath(".//div[@class='example']/div/div/div[1]/a[" + (i + 1) + "]")); + botonDeTurno.click(); + } + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/CheckBoxesTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/CheckBoxesTest.java new file mode 100644 index 0000000..b9047ac --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/CheckBoxesTest.java @@ -0,0 +1,36 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.ResultsPage; +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.SearchPage; +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import static junit.framework.TestCase.assertTrue; +import static org.junit.Assert.assertNull; + + +/** + * Unit test for simple App. + */ +public class CheckBoxesTest extends BaseTest { + + @Test + public void testApp() throws InterruptedException { + //1 + driver.navigate().to("https://the-internet.herokuapp.com/checkboxes"); + //2 + WebElement checkbox2 = + driver.findElement(By.xpath(".//*[@id='checkboxes']/input[2]")); + assertTrue(checkbox2.getAttribute("checked").equals("true")); + //2.1 + WebElement checkbox1 = + driver.findElement(By.xpath(".//*[@id='checkboxes']/input[1]")); + assertNull(checkbox1.getAttribute("checked")); + //3 + checkbox1.click(); + //4 + assertTrue(checkbox1.getAttribute("checked").equals("true")); + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/DisappearingElementsTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/DisappearingElementsTest.java new file mode 100644 index 0000000..bb4afcf --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/DisappearingElementsTest.java @@ -0,0 +1,30 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.ResultsPage; +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.SearchPage; +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import java.util.List; + + +/** + * Unit test for simple App. + */ +public class DisappearingElementsTest extends BaseTest { + + @Test + public void testApp() throws InterruptedException { + driver.navigate().to("https://the-internet.herokuapp.com/disappearing_elements"); + for(int i = 0; i < 10; i++) { + WebElement elementsDelMenu = + driver.findElement( + By.xpath(".//*[@id='content']/div/ul/li[last()]")); + elementsDelMenu.click(); + driver.navigate().back(); + driver.navigate().refresh(); + } + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/DragAndDropTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/DragAndDropTest.java new file mode 100644 index 0000000..ed7df38 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/DragAndDropTest.java @@ -0,0 +1,46 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.ResultsPage; +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.SearchPage; +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Actions; + + +/** + * Unit test for simple App. + */ +public class DragAndDropTest extends BaseTest { + + @Test + public void testApp() throws InterruptedException { + driver.navigate().to("http://compendiumdev.co.uk/selenium/testpages/gui_user_interactions.html"); + WebElement draggable1 = driver.findElement(By.xpath(".//*[@class='draganddrops']/div/div[1]")); + WebElement draggable2 = driver.findElement(By.id("draggable2")); + WebElement droppable1 = driver.findElement(By.id("droppable1")); + WebElement droppable2 = driver.findElement(By.id("droppable2")); + + Actions dragAndDrop = new Actions(driver); + dragAndDrop.dragAndDrop(draggable1,droppable1).perform(); + Thread.sleep(4000); + dragAndDrop.dragAndDrop(draggable2,droppable2).perform(); + Thread.sleep(4000); + + + /* + dragAndDrop + .clickAndHold(draggable2) + .moveToElement(droppable1) + .release().build().perform(); + Thread.sleep(4000); + + dragAndDrop + .clickAndHold(draggable2) + .moveToElement(droppable2) + .release().build().perform(); + Thread.sleep(4000); + */ + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/DropdownTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/DropdownTest.java new file mode 100644 index 0000000..9eb0732 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/DropdownTest.java @@ -0,0 +1,31 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.ResultsPage; +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.SearchPage; +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import java.util.List; + + +/** + * Unit test for simple App. + */ +public class DropdownTest extends BaseTest { + + @Test + public void testApp() throws InterruptedException { + driver + .navigate().to("https://the-internet.herokuapp.com/dropdown"); + List options = + driver.findElements(By.xpath(".//*[@id='dropdown']/option")); + for (int i = 1; i < options.size(); i++) { + WebElement option = + driver.findElement( + By.xpath(".//*[@id='dropdown']/option[" + (i + 1) + "]")); + option.click(); + } + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/DynamicContentTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/DynamicContentTest.java new file mode 100644 index 0000000..9f80c62 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/DynamicContentTest.java @@ -0,0 +1,41 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.ResultsPage; +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.SearchPage; +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import static junit.framework.TestCase.assertFalse; + + +/** + * Unit test for simple App. + */ +public class DynamicContentTest extends BaseTest { + + @Test + public void testApp() throws InterruptedException { + driver.navigate().to("https://the-internet.herokuapp.com/dynamic_content"); + WebElement imatge1 = + driver.findElement( + By.xpath(".//*[@id='content']/div[3]/div[1]/img")); + WebElement text1 = + driver.findElement( + By.xpath(".//*[@id='content']/div[3]/div[2]")); + String urlImatge1 = imatge1.getAttribute("src"); + String texttotal1 = text1.getText(); + driver.navigate().refresh(); + imatge1 = + driver.findElement( + By.xpath(".//*[@id='content']/div[3]/div[1]/img")); + text1 = + driver.findElement( + By.xpath(".//*[@id='content']/div[3]/div[2]")); + String urlImatge2 = imatge1.getAttribute("src"); + String texttotal2 = text1.getText(); + assertFalse(urlImatge1.equals(urlImatge2)); + assertFalse(texttotal1.equals(texttotal2)); + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/FramesTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/FramesTest.java new file mode 100644 index 0000000..92ded1e --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/FramesTest.java @@ -0,0 +1,39 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.ResultsPage; +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.SearchPage; +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; + +import java.util.List; + + +/** + * Unit test for simple App. + */ +public class FramesTest extends BaseTest { + + public void listIframesFromPage(WebDriver driver) { + final List iframes = driver.findElements(By.tagName("frame")); + for (WebElement iframe : iframes) { + System.out.println(iframe.getAttribute("id")); + System.out.println(iframe.getAttribute("name")); + } + } + + @Test + public void testApp() throws InterruptedException { + driver.navigate().to("https://the-internet.herokuapp.com/nested_frames"); + listIframesFromPage(driver); + driver.switchTo().frame("frame-bottom"); + System.out.println(driver.findElement(By.xpath("html/body")).getText()); + driver.switchTo().defaultContent(); + driver.switchTo().frame("frame-top"); + listIframesFromPage(driver); + driver.switchTo().frame("frame-middle"); + System.out.println(driver.findElement(By.xpath("html/body")).getText()); + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/HoverMenuTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/HoverMenuTest.java new file mode 100644 index 0000000..c577672 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/HoverMenuTest.java @@ -0,0 +1,50 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Actions; +import org.openqa.selenium.support.ui.ExpectedCondition; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +import java.util.List; + +import static junit.framework.TestCase.assertTrue; + + +/** + * Unit test for simple App. + */ +public class HoverMenuTest extends BaseTest { + + @Test + public void testApp() throws InterruptedException { + //1 + driver.get("http://opencart.votarem.lu"); + //2 + Actions hover = new Actions(driver); + List llistaCategories = driver.findElements(By.xpath(".//*[@id='menu']/div[2]/ul/li")); + System.out.println(llistaCategories.size()); + for (int i = 0; i < llistaCategories.size()-1; i++) { + WebElement categoriaAIterar = driver.findElement(By.xpath(".//*[@id='menu']/div[2]/ul/li[" + (i + 1) + "]")); + hover.moveToElement(categoriaAIterar).build().perform(); + /////// + List llistaElements = driver.findElements(By.xpath(".//*[@id='menu']/div[2]/ul/li[" + (i + 1) + "]/div/div/ul/li")); + String categoria = null; + for (int j = 0; j < llistaElements.size(); j++) { + WebElement elementAIterar = driver.findElement(By.xpath(".//*[@id='menu']/div[2]/ul/li[" + (i + 1) + "]/div/div/ul/li[" + (j + 1) + "]")); + hover.moveToElement(elementAIterar).build().perform(); + categoria = driver.findElement(By.xpath(".//*[@id='menu']/div[2]/ul/li[" + (i + 1) + "]/a")).getText(); + } + if (llistaElements.size() > 0) { + WebElement showAll = driver.findElement(By.xpath(".//*[@id='menu']/div[2]/ul/li[" + (i + 1) + "]/div/a")); + hover.moveToElement(showAll).build().perform(); + showAll.click(); + WebElement breadcrumb = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='product-category']/ul"))); + assertTrue(driver.findElement(By.xpath(".//*[@id='content']/h2")).getText().equals(categoria)); + } + } + + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/JWPlayer.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/JWPlayer.java new file mode 100644 index 0000000..61d3716 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/JWPlayer.java @@ -0,0 +1,205 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.ui.ExpectedCondition; +import org.openqa.selenium.support.ui.WebDriverWait; + +/** + * Created by guillemhernandezsola on 09/05/2017. + */ +public class JWPlayer extends BaseTest { + //Clase adaptada a JWPlayer 7 + /** + * JavaScript executor to fire the jwplayer methods + */ + protected JavascriptExecutor executor; + protected WebDriver driver; + + /** + * Constructor. + * + * @param driver WebDriver Element to use as executor for the JS functions + */ + public JWPlayer(WebDriver driver) { + this.driver = driver; + this.executor = (JavascriptExecutor) driver; + } + + /** + * Wait for the jwplayer to load on the page.
Timeout: 30 seconds
Polls every 5 seconds + * + * @throws InterruptedException + */ + protected void waitForPlayer(WebDriver driver) throws InterruptedException { + wait.pause(2000); + (new WebDriverWait(driver, 10)).until(new ExpectedCondition() { + public Boolean apply(WebDriver d) { + return (executor.executeScript("return jwplayer().getState()") != null); + } + }); + } + + public boolean assertStateRetornarDirecto() throws InterruptedException { + waitForPlayer(driver); + double pos = 0; + String strPos = ""; + boolean avanza = false; + String estado = String.valueOf(executor.executeScript("return jwplayer().getState()")); + strPos = String.valueOf(executor.executeScript("return jwplayer().getPosition()")); + pos = Double.parseDouble(strPos); + System.out.println(estado); + System.out.println(pos); + while (pos < -50.0) { + strPos = String.valueOf(executor.executeScript("return jwplayer().getPosition()")); + pos = Double.parseDouble(strPos); + estado = String.valueOf(executor.executeScript("return jwplayer().getState()")); + System.out.println(estado); + System.out.println(pos); + } + strPos = String.valueOf(executor.executeScript("return jwplayer().getPosition()")); + pos = Double.parseDouble(strPos); + if (pos > -50.0) avanza = true; + System.out.println(estado); + System.out.println(pos); + return avanza; + } + + public boolean rewind(int seconds) throws InterruptedException { + waitForPlayer(driver); + double initialPosition = 0; + double finalPos = 0; + String strPos = ""; + String estado = ""; + boolean rewind = false; + estado = String.valueOf(executor.executeScript("return jwplayer().getState()")); + strPos = String.valueOf(executor.executeScript("return jwplayer().getPosition()")); + initialPosition = Double.parseDouble(strPos); + int seek = (int) initialPosition - seconds; + System.out.println("rewind" + Integer.toString(seconds) + " - " + estado); + System.out.println("rewind initialPosition" + Integer.toString(seconds) + " - " + initialPosition); + System.out.println("jwplayer().seek(jwplayer().getPosition() - " + Integer.toString(seconds) + ")"); + executor.executeScript("jwplayer().seek(" + Integer.toString(seek) + ")"); + estado = String.valueOf(executor.executeScript("return jwplayer().getState()")); + strPos = String.valueOf(executor.executeScript("return jwplayer().getPosition()")); + initialPosition = Double.parseDouble(strPos); + System.out.println("assertStateRewind" + Integer.toString(seconds) + " - " + estado); + System.out.println("assertStateRewind currentPosition" + Integer.toString(seconds) + " - " + initialPosition); + while (initialPosition < finalPos) { + System.out.println("Ejecutando script rewind"); + System.out.println("jwplayer().seek(" + Integer.toString(seek) + ")"); + executor.executeScript("jwplayer().seek(" + Integer.toString(seek) + ")"); + strPos = String.valueOf(executor.executeScript("return jwplayer().getPosition()")); + finalPos = Double.parseDouble(strPos); + System.out.println("rewind" + Integer.toString(seconds) + " - " + estado); + System.out.println("rewind initialPosition" + Integer.toString(seconds) + " - " + initialPosition); + System.out.println("rewind currentPosition" + Integer.toString(seconds) + " - " + finalPos); + } + if (initialPosition >= finalPos) rewind = true; + strPos = String.valueOf(executor.executeScript("return jwplayer().getPosition()")); + System.out.println("rewind to position " + strPos); + return rewind; + } + + public boolean fastForward(int seconds) throws InterruptedException { + waitForPlayer(driver); + double initialPosition = 0; + double finalPos = 0; + String strPos = ""; + String estado = ""; + boolean avanza = false; + estado = String.valueOf(executor.executeScript("return jwplayer().getState()")); + strPos = String.valueOf(executor.executeScript("return jwplayer().getPosition()")); + initialPosition = Double.parseDouble(strPos); + System.out.println("fastForward" + Integer.toString(seconds) + " - " + estado); + System.out.println("fastForward initialPosition" + Integer.toString(seconds) + " - " + initialPosition); + System.out.println("jwplayer().seek(" + Integer.toString(seconds) + ")"); + executor.executeScript("jwplayer().seek(" + Integer.toString(seconds) + ")"); + estado = String.valueOf(executor.executeScript("return jwplayer().getState()")); + strPos = String.valueOf(executor.executeScript("return jwplayer().getPosition()")); + initialPosition = Double.parseDouble(strPos); + System.out.println("fastForward" + Integer.toString(seconds) + " - " + estado); + System.out.println("fastForward currentPosition" + Integer.toString(seconds) + " - " + initialPosition); + while (finalPos < seconds) { + System.out.println("Ejecutando script rewind"); + System.out.println("jwplayer().seek(" + Integer.toString(seconds) + ")"); + executor.executeScript("jwplayer().seek(" + Integer.toString(seconds) + ")"); + strPos = String.valueOf(executor.executeScript("return jwplayer().getPosition()")); + finalPos = Double.parseDouble(strPos); + System.out.println("fastForward" + Integer.toString(seconds) + " - " + estado); + System.out.println("fastForward currentPosition" + Integer.toString(seconds) + " - " + initialPosition); + System.out.println("fastForward finalPos" + Integer.toString(seconds) + " - " + finalPos); + } + if (finalPos >= seconds) avanza = true; + strPos = String.valueOf(executor.executeScript("return jwplayer().getPosition()")); + System.out.println("fastForward to position " + strPos); + return avanza; + } + + /** + * Asserts the state of the player is buffering or playing. + * + * @return TRUE if the current state of the player matches the expected one.
+ * FALSE if the current state of the player does not match the expected one. + * @throws InterruptedException + */ + public boolean assertStatePlaying() throws InterruptedException { + waitForPlayer(driver); + return executor.executeScript("return jwplayer().getState()").equals("buffering") || + executor.executeScript("return jwplayer().getState()").equals("playing"); + } + + public boolean assertStatePlay() throws InterruptedException { + waitForPlayer(driver); + executor.executeScript("jwplayer().play(true)"); + System.out.println(executor.executeScript("return jwplayer().getState()")); + return executor.executeScript("return jwplayer().getState()").equals("buffering") || + executor.executeScript("return jwplayer().getState()").equals("playing"); + } + + public boolean assertStateMuteTrue() throws InterruptedException { + waitForPlayer(driver); + executor.executeScript("jwplayer().setMute(true)"); + System.out.println(executor.executeScript("return jwplayer().getMute()")); + return executor.executeScript("return jwplayer().getMute()").equals(true); + } + + public boolean assertVolume(double volume) throws InterruptedException { + waitForPlayer(driver); + double vol = 0; + String strVol = ""; + executor.executeScript("jwplayer().setVolume(" + volume + ")"); + System.out.println(executor.executeScript("return jwplayer().getVolume()")); + strVol = String.valueOf(executor.executeScript("return jwplayer().getVolume()")); + vol = Double.parseDouble(strVol); + if (vol == volume) return true; + else return false; + } + + public boolean assertStateMuteFalse() throws InterruptedException { + waitForPlayer(driver); + executor.executeScript("jwplayer().setMute(false)"); + System.out.println(executor.executeScript("return jwplayer().getMute()")); + return executor.executeScript("return jwplayer().getMute()").equals(false); + } + + public boolean assertStatePause() throws InterruptedException { + waitForPlayer(driver); + executor.executeScript("jwplayer().pause()"); + System.out.println(executor.executeScript("return jwplayer().getState()")); + return executor.executeScript("return jwplayer().getState()").equals("buffering") || + executor.executeScript("return jwplayer().getState()").equals("paused"); + } + + public boolean assertStateRetrocede45mins() throws InterruptedException { + return rewind(2700); + } + + public boolean assertStateAvanza130secs() throws InterruptedException { + return fastForward(130); + } + + public boolean assertStateRetrocede30secs() throws InterruptedException { + return rewind(30); + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/JsExecutorTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/JsExecutorTest.java new file mode 100644 index 0000000..b7e7874 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/JsExecutorTest.java @@ -0,0 +1,75 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import org.junit.Test; +import org.openqa.selenium.*; + +import static org.junit.Assert.assertTrue; + + +/** + * Created by guillemhernandezsola on 09/05/2017. + */ +public class JsExecutorTest extends BaseTest { + + + @Test + public void testVideoAvanzado() throws Exception { + driver.get("http://www.ccma.cat/tv3/directe-avancat/324/"); + acceptarCookies(driver); + skipPublicitat(driver); + JWPlayer jw = new JWPlayer(driver); + assertTrue(jw.assertStatePlay()); + assertTrue(jw.assertStatePlaying()); + assertTrue(jw.assertStatePause()); + assertTrue(jw.assertStatePlay()); + assertTrue(jw.assertStateMuteTrue()); + assertTrue(jw.assertStateMuteFalse()); + assertTrue(jw.assertVolume(50)); + assertTrue(jw.assertVolume(0)); + assertTrue(jw.assertVolume(100)); + assertTrue(jw.assertStateRetrocede45mins()); + jse = (JavascriptExecutor) driver; + jse.executeScript("window.scrollBy(0,500)", ""); + By tornaDirecte = By.xpath(".//*[@class='torna-directe']/span"); + wait.forAsyncElement(driver.findElement(tornaDirecte), 10, 3); + System.out.println("Clicant al botó de directe"); + LocalNavigationActions.hoverAndClick(driver, tornaDirecte); + assertTrue(jw.assertStateRetornarDirecto()); + } + + //Suposem q s'ha fet un play al media + //fem un skip de la publicitat si es que existeix + //per poder fer controls sobre el media i que la publicitat no interfereixi + //llança exception si rebasem el llindar de publicitat demanat + public static void skipPublicitat(WebDriver driver) throws Exception { + String videoObject = ".//video[contains(@class,'jw-video')]"; + String adsObject = ".//*[contains(@class,'jw-flag-ads')]"; + wait.forElementPresent(By.xpath(videoObject)); + try { + if (wait.forAsyncElement(driver.findElement(By.xpath(adsObject)), 30, 3)) { + WebElement divAudioPlayer = driver.findElement(By.xpath(videoObject)); + System.out.println("Hi ha publicitat"); + String urlAds = divAudioPlayer.getAttribute("src"); + if (!urlAds.isEmpty()) + System.out.println("URL de la publicitat: " + urlAds); + wait.forElementNotVisible(By.xpath(adsObject)); + } else { + System.out.println("No hi ha publicitat"); + } + } catch (NoSuchElementException e) { + System.out.println("No hi ha publicitat"); + } catch (TimeoutException e) { + System.out.println("No hi ha publicitat"); + } + } + + public static void acceptarCookies(WebDriver driver) throws InterruptedException { + try { + driver.findElement(By.xpath("id('dismiss')/div")).click(); + } catch (NoSuchElementException e) { + System.out.println("No hem acceptat les cookies"); + } catch (TimeoutException e) { + System.out.println("No hem acceptat les cookies"); + } + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/LocalNavigationActions.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/LocalNavigationActions.java new file mode 100644 index 0000000..7fdc102 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/LocalNavigationActions.java @@ -0,0 +1,213 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import org.openqa.selenium.*; +import org.openqa.selenium.interactions.Actions; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * Created by guillem on 14/04/15. + */ +public class LocalNavigationActions extends Locators { + public static long timeOut = 120; + protected final WebDriver driver; + public final LocalRemoteWebDriverWait wait; + + public LocalNavigationActions(WebDriver driver) { + this.driver = driver; + this.wait = + new LocalRemoteWebDriverWait(driver, timeOut); + } + + public static void click(WebElement webElement) { + webElement.click(); + } + + public static void click(WebDriver driver, String objectId) { + driver.findElement(identifyLocationStrategy(objectId)).click(); + } + + public static WebElement getLastWebelement(WebDriver driver, String locator) { + List elements = driver.findElements(identifyLocationStrategy(locator)); + return elements.get(elements.size() - 1); + } + + public static WebElement getFirstWebelement(WebDriver driver, String locator) { + List elements = driver.findElements(identifyLocationStrategy(locator)); + return elements.get(0); + } + + public static int countElementsFromXpath(WebDriver driver, String locator) { + List elements = driver.findElements(identifyLocationStrategy(locator)); + return elements.size(); + } + + public static boolean elementExists(WebDriver driver, By by) { + try { + driver.findElement(by); + } catch (NoSuchElementException e) { + System.out.println(e); + return false; + } + return true; + } + + public static void hoverAndClick(WebDriver driver, WebElement webElement) { + Actions action = new Actions(driver); + action.moveToElement(webElement).moveToElement(webElement).click(webElement).build() + .perform(); + } + + public static void hoverAndClick(WebDriver driver, By by) { + Actions action = new Actions(driver); + action.moveToElement(driver.findElement(by)).moveToElement(driver.findElement(by)).click(driver.findElement(by)).build() + .perform(); + } + + public static void hoverAndClick(WebDriver driver, String objectId) { + Actions action = new Actions(driver); + WebElement elem = driver.findElement(identifyLocationStrategy(objectId)); + action.moveToElement(elem).moveToElement(elem).click(elem).build().perform(); + } + + public static void hoverElement(WebDriver driver, String objectId) { + Actions action = new Actions(driver); + WebElement elem = driver.findElement(identifyLocationStrategy(objectId)); + action.moveToElement(elem).moveToElement(elem).perform(); + + } + + public static void hoverElement(WebDriver driver, WebElement webElement) { + Actions action = new Actions(driver); + action.moveToElement(webElement).moveToElement(webElement).perform(); + } + + public static WebElement getLastWebelement(WebDriver driver, List webElements) { + return webElements.get(webElements.size() - 1); + } + + public static WebElement getFirstWebelement(WebDriver driver, List webElements) { + return webElements.get(0); + } + + public static int countElementsFromXpath(WebDriver driver, List webElements) { + return webElements.size(); + } + + public static boolean WebElementExists(WebDriver driver, WebElement webElement) { + driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS); + try { + webElement.isEnabled(); + } catch (NoSuchElementException e) { + driver.manage().timeouts() + .implicitlyWait(timeOut, + TimeUnit.MILLISECONDS); + System.out.println(e); + return false; + } + driver.manage().timeouts() + .implicitlyWait(timeOut, + TimeUnit.MILLISECONDS); + return true; + } + + public static boolean WebElementExists(WebDriver driver, WebElement webElement, boolean logInfo) { + driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS); + try { + webElement.isEnabled(); + } catch (NoSuchElementException e) { + driver.manage().timeouts() + .implicitlyWait(timeOut, + TimeUnit.MILLISECONDS); + if (logInfo) { + System.out.println(e); + } + return false; + } + driver.manage().timeouts() + .implicitlyWait(timeOut, + TimeUnit.MILLISECONDS); + return true; + } + + public static Boolean WaitForElement(WebDriver driver, WebElement element) { + try { + WebElement elementResponse = + (new WebDriverWait(driver, 10)).until(ExpectedConditions.visibilityOf(element)); + return WebElementExists(driver, elementResponse); + } catch (TimeoutException TimeOut) { + System.out.println(TimeOut); + return false; + } + } + + /** + * Method used check if a WebElement exists and it's displayed. + * + * @param driver + * @param elementToCheck + * @return + */ + public static boolean elementPresent(WebDriver driver, WebElement elementToCheck) { + if (WebElementExists(driver, elementToCheck) && elementToCheck.isDisplayed()) { + return true; + } else + return false; + } + + public static boolean elementPresent(WebDriver driver, WebElement elementToCheck, boolean logInfo) { + if (WebElementExists(driver, elementToCheck, logInfo) && elementToCheck.isDisplayed()) { + return true; + } else + return false; + } + + /** + * This method return the Xpath location from a (Xpath)WebElement return -1 if error + * + * @param element + * @return + */ + public static String getXpathLocatorFronWebElement(WebElement element) { + String xpahtLocation = element.toString(); + try { + xpahtLocation = + xpahtLocation.substring(xpahtLocation.indexOf("//"), xpahtLocation.length() - 1) + .trim(); + } catch (Exception e) { + System.out.println(e); + xpahtLocation = "-1"; + } + return xpahtLocation; + } + + /** + * this method wait for element be presents (20000) + * return true if the elemnt is found or false + * + * @param driver + * @param element + * @param wait + * @return + */ + public static boolean waitForElementPresent(WebDriver driver, WebElement element, + LocalRemoteWebDriverWait wait) { + int i = 0; + while (!WebElementExists(driver, element) && i < 20) { + wait.pause(1000); + i++; + } + return WebElementExists(driver, element); + } + + public void listIframesFromPage(WebDriver driver) { + final List iframes = driver.findElements(By.tagName("frame")); + for (WebElement iframe : iframes) { + System.out.println(iframe.getAttribute("id")); + System.out.println(iframe.getAttribute("name")); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/LocalRemoteWebDriverWait.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/LocalRemoteWebDriverWait.java new file mode 100644 index 0000000..a976fe0 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/LocalRemoteWebDriverWait.java @@ -0,0 +1,209 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import org.openqa.selenium.By; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.*; + +import java.util.Arrays; + +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; + +/** + * Wraps LocalRemoteWebDriverWait to be used in a more convenient way and adds functionality. + * + * @author guillem.hernandez + */ +public class LocalRemoteWebDriverWait extends WebDriverWait { + + private final WebDriver driver; + + public LocalRemoteWebDriverWait(WebDriver driver, long timeOutInSeconds) { + super(driver, timeOutInSeconds); + this.driver = driver; + } + + public static boolean isVisible(WebDriver driver, String locator) { + WebElement element = driver.findElement(Locators.identifyLocationStrategy(locator)); + return ExpectedConditions.visibilityOf(element) != null; + } + + public static ExpectedCondition isElementPresent(final WebElement element) { + ExpectedCondition elementPresent = new ExpectedCondition() { + public Boolean apply(WebDriver localDriver) { + return ExpectedConditions.visibilityOf(element) != null; + } + }; + return elementPresent; + } + + public boolean isElementPresent(By by) { + try { + driver.findElements(by); + return true; + } catch (org.openqa.selenium.NoSuchElementException e) { + return false; + } + } + + public Boolean forAsyncElement(WebElement e, long seconds, + int retries) { + // return true when the element is present + // Up to n retries times + for (int i = 0; i < retries; i++) { + // Check whether our element is there yet + if (e != null) { + return Boolean.valueOf(e.isDisplayed()); + } + pauseSeconds(seconds); + } + return Boolean.valueOf(null); + } + + public static boolean isElementPresent(WebDriver driver, String locator) { + WebElement element = driver.findElement(Locators.identifyLocationStrategy(locator)); + return ExpectedConditions.visibilityOf(element) != null; + } + + public void forElementPresent(WebElement element) { + until(isElementPresent(element)); + } + + public void forElementPresent(By locator) { + until(ExpectedConditions.presenceOfElementLocated(locator)); + } + + public void forTitle(String title) { + until(ExpectedConditions.titleIs(title)); + } + + public void forTitleContaining(String titleSubstr) { + until(ExpectedConditions.titleContains(titleSubstr)); + } + + public void forElementNotPresent(WebElement element) { + until(not(isElementPresent(element))); + } + + public void forAllElementsPresent(WebElement... elementsList) { + until(areAllElementsPresent(elementsList)); + } + + public void forElementToBeSelected(WebElement element) { + until(ExpectedConditions.elementToBeSelected(element)); + } + + public void forElementDisabled(WebElement element) { + until(ExpectedConditions.stalenessOf(element)); + } + + public void forElementVisible(By locator) { + forElementPresent(locator); + until(ExpectedConditions.visibilityOfElementLocated(locator)); + } + + public void forElementVisible(WebElement element) { + forElementPresent(element); + until(ExpectedConditions.visibilityOf(element)); + } + + public void forElementNotVisible(By locator) { + until(ExpectedConditions.invisibilityOfElementLocated(locator)); + } + + public void forElementToBeClickable(By locator) { + until(ExpectedConditions.elementToBeClickable(locator)); + } + + public void forElementToBeGone(WebElement element) { + until(ExpectedConditions.stalenessOf(element)); + } + + public void forTextPresent(By locator, String text) { + until(ExpectedConditions.textToBePresentInElement(locator, text)); + } + + public void forTextPresentInElementValue(By locator, String text) { + until(ExpectedConditions.textToBePresentInElementValue(locator, text)); + } + + public void forFrameAvailableAndSwitchToIt(String frameLocator) { + until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameLocator)); + } + + public Boolean forAsyncAttribute(WebElement e, String textValue, long milliseconds, + int retries) { + // return true when the element is present + // Up to n retries times + for (int i = 0; i < retries; i++) { + // Check whether our element is there yet + if (e.getAttribute(textValue) != null) { + return Boolean.valueOf(e.getAttribute(textValue)); + } + pause(milliseconds); + } + return Boolean.valueOf(null); + } + + public void forPageAndAjaxToLoad() { + until(isPageAndAjaxLoaded()); + } + + public ExpectedCondition areAllElementsPresent(final WebElement... elementsList) { + ExpectedCondition elementsPresent = new ExpectedCondition() { + public Boolean apply(WebDriver localDriver) { + return ExpectedConditions.visibilityOfAllElements(Arrays.asList(elementsList)) + != null; + } + }; + return elementsPresent; + } + + public ExpectedCondition not(final ExpectedCondition toInvert) { + return new ExpectedCondition() { + public Boolean apply(WebDriver localDriver) { + try { + Object result = toInvert.apply(driver); + return (result == null || result == Boolean.FALSE); + } catch (Exception e) { + System.out.println(e); + return true; + } + } + }; + } + + public ExpectedCondition isPageAndAjaxLoaded() { + ExpectedCondition pageLoaded = new ExpectedCondition() { + public Boolean apply(WebDriver localDriver) { + return ((JavascriptExecutor) localDriver) + .executeScript("return document.readyState").equals("complete"); + } + }; + return pageLoaded; + } + + public void pause(long milliseconds) { + try { + Sleeper.SYSTEM_SLEEPER.sleep(new Duration(milliseconds, MILLISECONDS)); + } catch (InterruptedException ie) { + System.out.println("Waiting finished during " + milliseconds + " milliseconds "); + } + } + + public void pauseSeconds(long seconds) { + try { + Sleeper.SYSTEM_SLEEPER.sleep(new Duration(seconds, SECONDS)); + } catch (InterruptedException ie) { + System.out.println("Waiting finished during " + seconds + " seconds "); + } + } + + // Leaving this here just in case we want to add extra actions in the future. + @Override + protected RuntimeException timeoutException(String message, Throwable lastException) { + return super.timeoutException(message, lastException); + } +} \ No newline at end of file diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/Locators.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/Locators.java new file mode 100644 index 0000000..df96839 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/Locators.java @@ -0,0 +1,21 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import org.openqa.selenium.By; + +/** + * Created by guillem on 14/04/15. + */ +public class Locators { + public static By identifyLocationStrategy(String objectId) { + By by = null; + if (objectId.startsWith("/") || objectId.startsWith("(") || objectId.startsWith(".//*")) { + by = By.xpath(objectId); + } else if (objectId.startsWith("div") || objectId.startsWith("span") || objectId + .startsWith("he") || objectId.startsWith("css")) { + by = By.cssSelector(objectId); + } else { + by = By.id(objectId); + } + return by; + } +} \ No newline at end of file diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/RegisterTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/RegisterTest.java new file mode 100644 index 0000000..9f8e307 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/RegisterTest.java @@ -0,0 +1,47 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.ResultsPage; +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.SearchPage; +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Actions; +import org.openqa.selenium.support.ui.ExpectedConditions; + +import static junit.framework.TestCase.assertTrue; + + +/** + * Unit test for simple App. + */ +public class RegisterTest extends BaseTest { + + @Test + public void testApp() throws InterruptedException { + //1 + driver.get("http://opencart.votarem.lu"); + //2 + Actions hover = new Actions(driver); + WebElement ninot = driver.findElement(By.xpath(".//*[@id='top-links']/ul/li[2]/a")); + hover.moveToElement(ninot).build().perform(); + //3 + ninot.click(); + wait.until(ExpectedConditions + .visibilityOfElementLocated(By + .xpath(".//*[@id='top-links']/ul/li[2]/ul"))); + //4 + WebElement register = driver.findElement(By.xpath(".//*[@id='top-links']/ul/li[2]/ul/li[1]/a")); + hover.moveToElement(register).build().perform(); + //5 + register.click(); + //6 + wait.until(ExpectedConditions + .visibilityOfElementLocated(By.id("content"))); + WebElement registerAccount = driver.findElement(By.id("account-register")); + assertTrue(registerAccount.isDisplayed()); + + + + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/SearchTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/SearchTest.java new file mode 100644 index 0000000..53dae57 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/SearchTest.java @@ -0,0 +1,56 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Actions; +import org.openqa.selenium.support.ui.ExpectedConditions; + +import java.util.List; + +import static junit.framework.TestCase.assertTrue; + + +/** + * Unit test for simple App. + */ +public class SearchTest extends BaseTest { + + @Test + public void testApp() throws InterruptedException { + String cerca = "apple"; + //1 + driver.get("http://opencart.votarem.lu"); + //2 + WebElement searchInput = driver.findElement(By.xpath(".//*[@id='search']/input")); + searchInput.click(); + searchInput.sendKeys(cerca); + //searchInput.submit(); + WebElement lupa = driver + .findElement(By.xpath(".//*[@id='search']/span/button")); + //hover.moveToElement(lupa).build().perform(); + lupa.click(); + String breadCrumbCerca = ".//*[@id='product-search']/ul"; + wait.until(ExpectedConditions + .visibilityOfElementLocated(By + .xpath(breadCrumbCerca))); + WebElement searchCriteria = + driver.findElement(By.xpath(".//*[@id='content']/h1")); + String keyword = searchCriteria.getText(); + assertTrue(keyword.contains(cerca)); + WebElement inputSearch = driver.findElement(By.id("input-search")); + assertTrue(inputSearch.getAttribute("value").contains(cerca)); + + + List resultats = + driver.findElements(By.xpath(".//*[@id='content']/div[3]/div")); + + for (int i = 0; i < resultats.size(); i++) { + WebElement titol = driver + .findElement(By + .xpath(".//*[@id='content']/div[3]/div[" + (i + 1) + "]/div/div[2]/div[1]/h4/a")); + assertTrue(titol.getText().toLowerCase().contains(cerca)); + } + + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/TestABTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/TestABTest.java new file mode 100644 index 0000000..2891358 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/TestABTest.java @@ -0,0 +1,36 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.ResultsPage; +import com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage.SearchPage; +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import static junit.framework.TestCase.assertTrue; + + +/** + * Unit test for simple App. + */ +public class TestABTest extends BaseTest { + + @Test + public void testApp() throws InterruptedException { + int counter1=0, counter2=0; + for(int i=0; i < 1000; i++){ + driver.get("https://the-internet.herokuapp.com/abtest"); + WebElement title = + driver.findElement(By.xpath(".//*[@id='content']/div/h3")); + if(title.getText().equals("A/B Test Variation 1")){ + counter1++; + } + else{ + counter2++; + } + driver.manage().deleteAllCookies(); + } + System.out.println("Han anat a la versio 1 "+counter1); + System.out.println("Han anat a la versio 2 "+counter2); + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/UploadTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/UploadTest.java new file mode 100644 index 0000000..212a5b6 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/UploadTest.java @@ -0,0 +1,33 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.ExpectedCondition; +import org.openqa.selenium.support.ui.ExpectedConditions; + +import java.io.File; +import java.util.List; + +import static junit.framework.TestCase.assertTrue; + + +/** + * Unit test for simple App. + */ +public class UploadTest extends BaseTest { + + @Test + public void testApp() throws InterruptedException { + driver.navigate().to("https://the-internet.herokuapp.com/upload"); + WebElement fileUpload = driver.findElement(By.id("file-upload")); + File file = new File("src" + File.separator + "main" + File.separator + "resources" + File.separator + "2-logo-B_activa.png"); + fileUpload.sendKeys(file.getAbsolutePath()); + WebElement buttonUpload = driver.findElement(By.id("file-submit")); + buttonUpload.click(); + WebElement uploadedFiles = driver.findElement(By.id("uploaded-files")); + wait.until(ExpectedConditions.visibilityOf(uploadedFiles)); + assertTrue(uploadedFiles.isDisplayed()); + } + +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/iFramesTest.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/iFramesTest.java new file mode 100644 index 0000000..7b74fe6 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/iFramesTest.java @@ -0,0 +1,35 @@ +package com.itnove.trainings.junit.startUsingWebDriver; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; + +import java.util.List; + + +/** + * Unit test for simple App. + */ +public class iFramesTest extends BaseTest { + + public void listIframesFromPage(WebDriver driver) { + final List iframes = driver.findElements(By.tagName("iframe")); + for (WebElement iframe : iframes) { + System.out.println(iframe.getAttribute("id")); + System.out.println(iframe.getAttribute("name")); + } + } + + @Test + public void testApp() throws InterruptedException { + driver.navigate().to("https://the-internet.herokuapp.com/iframe"); + listIframesFromPage(driver); + driver.switchTo().frame("mce_0_ifr"); + WebElement editor = driver.findElement(By.xpath(".//*[@id='tinymce']")); + hover.moveToElement(editor).click().build().perform(); + editor.clear(); + editor.sendKeys("Podem agafar menjar els de la de Media?"); + Thread.sleep(3000); + } +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/pages/searchPage/ResultsPage.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/pages/searchPage/ResultsPage.java new file mode 100644 index 0000000..5657fd0 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/pages/searchPage/ResultsPage.java @@ -0,0 +1,33 @@ +package com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +/** + * Created by guillem on 01/03/16. + */ +public class ResultsPage { + + private WebDriver driver; + + @FindBy(id = "links_wrapper") + public WebElement resultsList; + + @FindBy(xpath = ".//*[@id='r1-0']/div") + public WebElement firstResult; + + public boolean isResultsListPresent() { + return resultsList.isDisplayed(); + } + + public void clickOnFirstResult() { + firstResult.click(); + } + + public ResultsPage(WebDriver driver) { + PageFactory.initElements(driver, this); + } + +} diff --git a/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/pages/searchPage/SearchPage.java b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/pages/searchPage/SearchPage.java new file mode 100644 index 0000000..81097f1 --- /dev/null +++ b/src/test/java/com/itnove/trainings/junit/startUsingWebDriver/pages/searchPage/SearchPage.java @@ -0,0 +1,30 @@ +package com.itnove.trainings.junit.startUsingWebDriver.pages.searchPage; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +/** + * Created by guillem on 01/03/16. + */ +public class SearchPage { + + private WebDriver driver; + + @FindBy(id = "search_form_input_homepage") + public WebElement searchBox; + + @FindBy(id = "search_button_homepage") + public WebElement searchButton; + + public SearchPage(WebDriver driver) { + PageFactory.initElements(driver, this); + } + + public void searchKeyword(String keyword) { + searchBox.clear(); + searchBox.sendKeys(keyword); + searchButton.click(); + } +}