Refactored

This commit is contained in:
Guillem Hernandez Sola
2019-08-15 11:01:06 +02:00
parent ac3e6652cd
commit e6b8b71ee7
30 changed files with 14 additions and 1444 deletions

View File

@@ -40,7 +40,7 @@ java -Dwebdriver.chrome.driver=chromedriver -Dwebdriver.gecko.driver=geckodriver
## Support
This tutorial is released into the public domain by ITNove under WTFPL.
This tutorial is released into the public domain by Agile611 under WTFPL.
[![WTFPL](http://www.wtfpl.net/wp-content/uploads/2012/12/wtfpl-badge-1.png)](http://www.wtfpl.net/)

View File

@@ -2,9 +2,9 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itnove.trainings.webdriver</groupId>
<artifactId>StartUsingWebDriver</artifactId>
<version>19.06.14</version>
<groupId>com.agile611.trainings.webdriver</groupId>
<artifactId>cursoSeleniumWebdriver</artifactId>
<version>19.08.15</version>
<build>
<plugins>
<plugin>

View File

@@ -0,0 +1,9 @@
package com.agile611.testng.webdriver;
public class App {
public String getWish() {
return "Hello";
}
}

View File

@@ -1,13 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
/**
* Hello world!
*
*/
public class App {
public String getWish() {
return "Hello";
}
}

View File

@@ -4,7 +4,7 @@
parallel="tests">
<test name="all" parallel="classes" thread-count="4" time-out="240000">
<packages>
<package name="com.itnove.trainings.testng.startUsingWebDriver.*"/>
<package name="com.agile611.testng.webdriver.*"/>
</packages>
</test>
</suite>

View File

@@ -1,69 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
/**
* Alerts Test from The Internet
*/
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);
}
}

View File

@@ -1,26 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import com.itnove.trainings.testng.startUsingWebDriver.pages.searchPage.ResultsPage;
import com.itnove.trainings.testng.startUsingWebDriver.pages.searchPage.SearchPage;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* Initial example appTest
*/
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"));
}
}

View File

@@ -1,58 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
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;
@BeforeMethod
public void setUp() throws MalformedURLException {
String browser = System.getProperty("browser");
String hub = System.getProperty("hub");
DesiredCapabilities capabilities;
if (browser != null && browser.equalsIgnoreCase("chrome")) {
capabilities = DesiredCapabilities.chrome();
System.setProperty("webdriver.chrome.driver", "src" + File.separator + "main" + File.separator + "resources" + File.separator + "chromedriver-macos");
//driver = new ChromeDriver(capabilities);
} else {
capabilities = DesiredCapabilities.firefox();
System.setProperty("webdriver.gecko.driver",
"src" + File.separator + "main"
+ File.separator + "resources"
+ File.separator + "geckodriver-macos");
//driver = new FirefoxDriver(capabilities);
}
driver = new RemoteWebDriver(new URL("http://0.0.0.0:4444/wd/hub"), capabilities);
//driver = new RemoteWebDriver(new URL(hub), 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);
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}

View File

@@ -1,17 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.testng.annotations.Test;
/**
* Basic Auth from the Internet
*/
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);
}
}

View File

@@ -1,28 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import java.util.List;
/**
* Challengin Dom from the Internet Heroku App
*/
public class ChallengingDomTest extends BaseTest {
@Test
public void testApp() throws InterruptedException {
driver.navigate().to("https://the-internet.herokuapp.com/challenging_dom");
List<WebElement> 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();
}
}
}

View File

@@ -1,33 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import static org.testng.Assert.assertTrue;
import static org.testng.AssertJUnit.assertNull;
/**
* Checkboxes from the Internet Heroku 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"));
}
}

View File

@@ -1,25 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
/**
* Disappearing Elements from The Internet HerokuApp
*/
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();
}
}
}

View File

@@ -1,43 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
/**
* Drag and Drop test from Compendium Dev
*/
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);
*/
}
}

View File

@@ -1,28 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import java.util.List;
/**
* Drop Down from the Internet Heroku App
*/
public class DropdownTest extends BaseTest {
@Test(enabled = false)
public void testApp() throws InterruptedException {
driver
.navigate().to("https://the-internet.herokuapp.com/dropdown");
List<WebElement> 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();
}
}
}

View File

@@ -1,38 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import static org.testng.Assert.assertFalse;
/**
* Dynamic Content from The Internet Heroku 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));
}
}

View File

@@ -1,36 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.testng.annotations.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<WebElement> 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());
}
}

View File

@@ -1,48 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.testng.annotations.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 org.testng.Assert.assertTrue;
/**
* Unit test for simple App.
*/
public class HoverMenuTest extends BaseTest {
@Test(enabled = false)
public void testApp() throws InterruptedException {
//1
driver.get("http://opencart.votarem.lu");
//2
Actions hover = new Actions(driver);
List<WebElement> 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<WebElement> 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));
}
}
}
}

View File

@@ -1,205 +0,0 @@
package com.itnove.trainings.testng.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.<br> Timeout: 30 seconds<br>Polls every 5 seconds
*
* @throws InterruptedException
*/
protected void waitForPlayer(WebDriver driver) throws InterruptedException {
wait.pauseMilliseconds(2000);
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
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 <code>TRUE</code> if the current state of the player matches the expected one.<br>
* <code>FALSE</code> 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().pauseMilliseconds()");
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);
}
}

View File

@@ -1,74 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.testng.annotations.Test;
import org.openqa.selenium.*;
import static org.testng.Assert.assertTrue;
/**
* Created by guillemhernandezsola on 09/05/2017.
*/
public class JsExecutorTest extends BaseTest {
@Test(enabled = false)
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");
}
}
}

View File

@@ -1,213 +0,0 @@
package com.itnove.trainings.testng.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<WebElement> elements = driver.findElements(identifyLocationStrategy(locator));
return elements.get(elements.size() - 1);
}
public static WebElement getFirstWebelement(WebDriver driver, String locator) {
List<WebElement> elements = driver.findElements(identifyLocationStrategy(locator));
return elements.get(0);
}
public static int countElementsFromXpath(WebDriver driver, String locator) {
List<WebElement> 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<WebElement> webElements) {
return webElements.get(webElements.size() - 1);
}
public static WebElement getFirstWebelement(WebDriver driver, List<WebElement> webElements) {
return webElements.get(0);
}
public static int countElementsFromXpath(WebDriver driver, List<WebElement> 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.pauseMilliseconds(1000);
i++;
}
return WebElementExists(driver, element);
}
public void listIframesFromPage(WebDriver driver) {
final List<WebElement> iframes = driver.findElements(By.tagName("frame"));
for (WebElement iframe : iframes) {
System.out.println(iframe.getAttribute("id"));
System.out.println(iframe.getAttribute("name"));
}
}
}

View File

@@ -1,203 +0,0 @@
package com.itnove.trainings.testng.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 java.util.concurrent.TimeUnit;
/**
* 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<Boolean> isElementPresent(final WebElement element) {
ExpectedCondition<Boolean> elementPresent = new ExpectedCondition<Boolean>() {
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 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));
}
pauseMilliseconds(milliseconds);
}
return Boolean.valueOf(null);
}
public void forPageAndAjaxToLoad() {
until(isPageAndAjaxLoaded());
}
public ExpectedCondition<Boolean> areAllElementsPresent(final WebElement... elementsList) {
ExpectedCondition<Boolean> elementsPresent = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver localDriver) {
return ExpectedConditions.visibilityOfAllElements(Arrays.asList(elementsList))
!= null;
}
};
return elementsPresent;
}
public ExpectedCondition<Boolean> not(final ExpectedCondition<?> toInvert) {
return new ExpectedCondition<Boolean>() {
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<Boolean> isPageAndAjaxLoaded() {
ExpectedCondition<Boolean> pageLoaded = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver localDriver) {
return ((JavascriptExecutor) localDriver)
.executeScript("return document.readyState").equals("complete");
}
};
return pageLoaded;
}
public void pauseMilliseconds(long milliseconds) {
try {
TimeUnit.MILLISECONDS.sleep(milliseconds);
} catch (InterruptedException ie) {
System.out.println("Waiting finished during " + milliseconds + " milliseconds ");
}
}
public void pauseSeconds(long seconds) {
try {
TimeUnit.SECONDS.sleep(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);
}
}

View File

@@ -1,21 +0,0 @@
package com.itnove.trainings.testng.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;
}
}

View File

@@ -1,44 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.testng.annotations.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 org.testng.Assert.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());
}
}

View File

@@ -1,55 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.util.List;
import static org.testng.Assert.assertTrue;
/**
* Unit test for simple App.
*/
public class SearchTest extends BaseTest {
@Test(enabled = false)
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<WebElement> 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));
}
}
}

View File

@@ -1,33 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import static org.testng.Assert.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 < 10; 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);
}
}

View File

@@ -1,31 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.io.File;
import static org.testng.Assert.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());
}
}

View File

@@ -1,35 +0,0 @@
package com.itnove.trainings.testng.startUsingWebDriver;
import org.testng.annotations.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<WebElement> iframes = driver.findElements(By.tagName("iframe"));
for (WebElement iframe : iframes) {
System.out.println(iframe.getAttribute("id"));
System.out.println(iframe.getAttribute("name"));
}
}
@Test(enabled = false)
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);
}
}

View File

@@ -1,33 +0,0 @@
package com.itnove.trainings.testng.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);
}
}

View File

@@ -1,30 +0,0 @@
package com.itnove.trainings.testng.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();
}
}