Revert "BAEL-4134"

This commit is contained in:
Loredana Crusoveanu
2020-07-07 14:18:10 +03:00
committed by GitHub
parent dffa1f64e6
commit 485b4e3e99
2477 changed files with 9477 additions and 547819 deletions

View File

@@ -1,14 +1,12 @@
package main.java.com.baeldung.selenium;
import main.java.com.baeldung.selenium.config.SeleniumConfig;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
package com.baeldung.selenium;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import com.baeldung.selenium.config.SeleniumConfig;
public class SeleniumExample {
@@ -17,15 +15,18 @@ public class SeleniumExample {
public SeleniumExample() {
config = new SeleniumConfig();
config.getDriver().get(url);
config.getDriver()
.get(url);
}
public void closeWindow() {
this.config.getDriver().close();
this.config.getDriver()
.close();
}
public String getTitle() {
return this.config.getDriver().getTitle();
return this.config.getDriver()
.getTitle();
}
public void getAboutBaeldungPage() {
@@ -35,29 +36,35 @@ public class SeleniumExample {
}
private void closeOverlay() {
List<WebElement> webElementList = this.config.getDriver().findElements(By.tagName("a"));
List<WebElement> webElementList = this.config.getDriver()
.findElements(By.tagName("a"));
if (webElementList != null) {
webElementList.stream()
.filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title")))
.filter(WebElement::isDisplayed)
.findAny()
.ifPresent(WebElement::click);
.filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title")))
.filter(WebElement::isDisplayed)
.findAny()
.ifPresent(WebElement::click);
}
}
private void clickAboutLink() {
this.config.getDriver().findElement(By.partialLinkText("About")).click();
Actions actions = new Actions(config.getDriver());
WebElement aboutElement = this.config.getDriver()
.findElement(By.id("menu-item-6138"));
actions.moveToElement(aboutElement).perform();
}
private void clickAboutUsLink() {
Actions builder = new Actions(config.getDriver());
WebElement element = this.config.getDriver().findElement(By.partialLinkText("About Baeldung."));
builder.moveToElement(element).build().perform();
WebElement element = this.config.getDriver()
.findElement(By.partialLinkText("About Baeldung."));
element.click();
}
public boolean isAuthorInformationAvailable() {
return this.config.getDriver()
.findElement(By.cssSelector("article > .row > div"))
.isDisplayed();
.getPageSource()
.contains("Hey ! I'm Eugen");
}
}

View File

@@ -1,17 +1,14 @@
package main.java.com.baeldung.selenium.config;
package com.baeldung.selenium.config;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.File;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SeleniumConfig {
private WebDriver driver;
@@ -19,15 +16,17 @@ public class SeleniumConfig {
public SeleniumConfig() {
Capabilities capabilities = DesiredCapabilities.firefox();
driver = new FirefoxDriver(capabilities);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage()
.timeouts()
.implicitlyWait(5, TimeUnit.SECONDS);
}
static {
System.setProperty("webdriver.gecko.driver", findFile("geckodriver.mac"));
}
static private String findFile(String filename) {
String paths[] = {"", "bin/", "target/classes"}; // if you have chromedriver somewhere else on the path, then put it here.
private static String findFile(String filename) {
String[] paths = { "", "bin/", "target/classes" }; // if you have chromedriver somewhere else on the path, then put it here.
for (String path : paths) {
if (new File(path + filename).exists())
return path + filename;
@@ -40,7 +39,8 @@ public class SeleniumConfig {
}
public void navigateTo(String url) {
driver.navigate().to(url);
driver.navigate()
.to(url);
}
public void clickElement(WebElement element) {

View File

@@ -1,7 +1,7 @@
package main.java.com.baeldung.selenium.models;
package com.baeldung.selenium.models;
import main.java.com.baeldung.selenium.config.SeleniumConfig;
import main.java.com.baeldung.selenium.pages.BaeldungAboutPage;
import com.baeldung.selenium.config.SeleniumConfig;
import com.baeldung.selenium.pages.BaeldungAboutPage;
import org.openqa.selenium.support.PageFactory;
public class BaeldungAbout {

View File

@@ -1,4 +1,4 @@
package main.java.com.baeldung.selenium.pages;
package com.baeldung.selenium.pages;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

View File

@@ -1,6 +1,6 @@
package main.java.com.baeldung.selenium.pages;
package com.baeldung.selenium.pages;
import main.java.com.baeldung.selenium.config.SeleniumConfig;
import com.baeldung.selenium.config.SeleniumConfig;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
@@ -8,7 +8,7 @@ import org.openqa.selenium.support.PageFactory;
public class BaeldungHomePage {
private SeleniumConfig config;
@FindBy(css=".header--menu > a")
@FindBy(css = ".nav--logo_mobile")
private WebElement title;
@FindBy(css = ".menu-start-here > a")
private WebElement startHere;

View File

@@ -1,6 +1,6 @@
package main.java.com.baeldung.selenium.pages;
package com.baeldung.selenium.pages;
import main.java.com.baeldung.selenium.config.SeleniumConfig;
import com.baeldung.selenium.config.SeleniumConfig;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

View File

@@ -1,4 +1,4 @@
package java.com.baeldung.selenium.clickusingjavascript;
package com.baeldung.selenium.clickusingjavascript;
import org.junit.After;
import org.junit.Before;
@@ -14,16 +14,18 @@ import org.openqa.selenium.support.ui.WebDriverWait;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class SeleniumJavaScriptClickTest {
import java.io.File;
public class SeleniumJavaScriptClickLiveTest {
private WebDriver driver;
private WebDriverWait wait;
@Before
public void setUp() {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
System.setProperty("webdriver.chrome.driver", new File("src/main/resources/chromedriver.mac").getAbsolutePath());
driver = new ChromeDriver();
wait = new WebDriverWait(driver, 5000);
wait = new WebDriverWait(driver, 20);
}
@After
@@ -37,19 +39,21 @@ public class SeleniumJavaScriptClickTest {
String title = driver.getTitle();
assertEquals("Baeldung | Java, Spring and Web Development tutorials", title);
wait.until(ExpectedConditions.elementToBeClickable(By.className("menu-search")));
WebElement searchButton = driver.findElement(By.className("menu-search"));
wait.until(ExpectedConditions.elementToBeClickable(By.className("nav--menu_item_anchor")));
WebElement searchButton = driver.findElement(By.className("nav--menu_item_anchor"));
clickElement(searchButton);
wait.until(ExpectedConditions.elementToBeClickable(By.id("search")));
WebElement searchInput = driver.findElement(By.id("search"));
searchInput.sendKeys("Selenium");
wait.until(ExpectedConditions.elementToBeClickable(By.className("btn-search")));
WebElement seeSearchResultsButton = driver.findElement(By.className("btn-search"));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".btn-search")));
WebElement seeSearchResultsButton = driver.findElement(By.cssSelector(".btn-search"));
clickElement(seeSearchResultsButton);
int seleniumPostsCount = driver.findElements(By.className("post")).size();
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("post")));
int seleniumPostsCount = driver.findElements(By.className("post"))
.size();
assertTrue(seleniumPostsCount > 0);
}

View File

@@ -1,4 +1,16 @@
package test.java.com.baeldung.selenium.junit;
package com.baeldung.selenium.cookies;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import java.io.File;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
@@ -9,12 +21,6 @@ import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class SeleniumCookiesJUnitLiveTest {
private WebDriver driver;
@@ -22,11 +28,21 @@ public class SeleniumCookiesJUnitLiveTest {
@Before
public void setUp() {
System.setProperty("webdriver.gecko.driver", findFile("geckodriver.mac"));
Capabilities capabilities = DesiredCapabilities.firefox();
driver = new FirefoxDriver(capabilities);
navUrl = "https://baeldung.com";
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
}
private static String findFile(String filename) {
String[] paths = { "", "bin/", "target/classes" }; // if you have chromedriver somewhere else on the path, then put it here.
for (String path : paths) {
if (new File(path + filename).exists())
return path + filename;
}
return "";
}
@After

View File

@@ -1,16 +1,21 @@
package test.java.com.baeldung.selenium.junit;
package com.baeldung.selenium.junit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import main.java.com.baeldung.selenium.SeleniumExample;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.testng.Assert.*;
import com.baeldung.selenium.SeleniumExample;
public class SeleniumWithJUnitLiveTest {
private static SeleniumExample seleniumExample;
private String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials";
private String expectedTitle = "About Baeldung | Baeldung";
@BeforeClass
public static void setUp() {
@@ -18,17 +23,17 @@ public class SeleniumWithJUnitLiveTest {
}
@AfterClass
public static void tearDown() {
public static void tearDown() throws IOException {
seleniumExample.closeWindow();
}
@Test
public void whenAboutBaeldungIsLoaded_thenAboutEugenIsMentionedOnPage() {
seleniumExample.getAboutBaeldungPage();
String actualTitle = seleniumExample.getTitle();
assertNotNull(actualTitle);
assertEquals(expectedTitle, actualTitle);
assertTrue(seleniumExample.isAuthorInformationAvailable());
seleniumExample.getAboutBaeldungPage();
String actualTitle = seleniumExample.getTitle();
assertNotNull(actualTitle);
assertEquals(expectedTitle, actualTitle);
assertTrue(seleniumExample.isAuthorInformationAvailable());
}
}

View File

@@ -1,9 +1,9 @@
package test.java.com.baeldung.selenium.junit;
package com.baeldung.selenium.pages;
import main.java.com.baeldung.selenium.config.SeleniumConfig;
import main.java.com.baeldung.selenium.models.BaeldungAbout;
import main.java.com.baeldung.selenium.pages.BaeldungHomePage;
import main.java.com.baeldung.selenium.pages.StartHerePage;
import com.baeldung.selenium.config.SeleniumConfig;
import com.baeldung.selenium.models.BaeldungAbout;
import com.baeldung.selenium.pages.BaeldungHomePage;
import com.baeldung.selenium.pages.StartHerePage;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

View File

@@ -0,0 +1,85 @@
package com.baeldung.selenium.screenshot;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.coordinates.WebDriverCoordsProvider;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class TakeScreenShotSeleniumLiveTest {
private static ChromeDriver driver;
@BeforeClass
public static void setUp() {
System.setProperty("webdriver.chrome.driver", resolveResourcePath("chromedriver.mac"));
Capabilities capabilities = DesiredCapabilities.chrome();
driver = new ChromeDriver(capabilities);
driver.manage()
.timeouts()
.implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://www.google.com/");
}
@AfterClass
public static void tearDown() throws IOException {
driver.close();
System.clearProperty("webdriver.chrome.driver");
}
@Test
public void whenGoogleIsLoaded_thenCaptureScreenshot() throws IOException {
takeScreenshot(resolveTestResourcePath("google-home.png"));
assertTrue(new File(resolveTestResourcePath("google-home.png")).exists());
}
@Test
public void whenGoogleIsLoaded_thenCaptureLogo() throws IOException {
WebElement logo = driver.findElement(By.id("hplogo"));
Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000))
.coordsProvider(new WebDriverCoordsProvider())
.takeScreenshot(driver, logo);
ImageIO.write(screenshot.getImage(), "jpg", new File(resolveTestResourcePath("google-logo.png")));
assertTrue(new File(resolveTestResourcePath("google-logo.png")).exists());
}
public void takeScreenshot(String pathname) throws IOException {
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File(pathname));
}
private static String resolveResourcePath(String filename) {
File file = new File("src/main/resources/" + filename);
return file.getAbsolutePath();
}
private static String resolveTestResourcePath(String filename) {
File file = new File("src/test/resources/" + filename);
return file.getAbsolutePath();
}
}

View File

@@ -1,16 +1,19 @@
package test.java.com.baeldung.selenium.testng;
package com.baeldung.selenium.testng;
import com.baeldung.selenium.SeleniumExample;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import main.java.com.baeldung.selenium.SeleniumExample;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
public class SeleniumWithTestNGLiveTest {
private SeleniumExample seleniumExample;
private String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials";
private String expectedTitle = "About Baeldung | Baeldung";
@BeforeSuite
public void setUp() {