Fix/update selenium config (#2095)

* Add project for hibernate immutable article
Add Event entity
Add hibernate configuration file
Add hibernateutil for configuration
Add test to match snippets from article

* Update master

* Selenium congif fix
Change chromedriver files to geckodriver
Use Firefox webdriver to stick with standard
Update tests for JUnit and TestNG
Modify SeleniumExample and move all WebDriver Configuration to corresponding class
Update selectors and copy content
This commit is contained in:
Walter Gómez 2017-06-16 01:13:36 -06:00 committed by Grzegorz Piwowarek
parent 3a04c3adc1
commit 9cf533654d
9 changed files with 25 additions and 37 deletions

Binary file not shown.

View File

@ -66,6 +66,6 @@
<properties> <properties>
<testng.version>6.10</testng.version> <testng.version>6.10</testng.version>
<selenium-java.version>3.0.1</selenium-java.version> <selenium-java.version>3.4.0</selenium-java.version>
</properties> </properties>
</project> </project>

View File

@ -1,32 +1,31 @@
package main.java.com.baeldung.selenium; package main.java.com.baeldung.selenium;
import main.java.com.baeldung.selenium.config.SeleniumConfig;
import org.openqa.selenium.By; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class SeleniumExample { public class SeleniumExample {
private WebDriver webDriver; private SeleniumConfig config;
private String url = "http://www.baeldung.com/"; private String url = "http://www.baeldung.com/";
public SeleniumExample() { public SeleniumExample() {
System.setProperty("webdriver.firefox.marionette", "C:\\selenium\\geckodriver.exe"); config = new SeleniumConfig();
webDriver = new FirefoxDriver(); config.getDriver().get(url);
webDriver.manage().window().maximize();
webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
webDriver.get(url);
} }
public void closeWindow() { public void closeWindow() {
webDriver.close(); this.config.getDriver().close();
} }
public String getTitle() { public String getTitle() {
return webDriver.getTitle(); return this.config.getDriver().getTitle();
} }
public void getAboutBaeldungPage() { public void getAboutBaeldungPage() {
@ -36,7 +35,7 @@ public class SeleniumExample {
} }
private void closeOverlay() { private void closeOverlay() {
List<WebElement> webElementList = webDriver.findElements(By.tagName("a")); List<WebElement> webElementList = this.config.getDriver().findElements(By.tagName("a"));
if (webElementList != null) { if (webElementList != null) {
webElementList.stream() webElementList.stream()
.filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title"))) .filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title")))
@ -47,14 +46,18 @@ public class SeleniumExample {
} }
private void clickAboutLink() { private void clickAboutLink() {
webDriver.findElement(By.partialLinkText("About")).click(); this.config.getDriver().findElement(By.partialLinkText("About")).click();
} }
private void clickAboutUsLink() { private void clickAboutUsLink() {
webDriver.findElement(By.partialLinkText("About Baeldung.")).click(); Actions builder = new Actions(config.getDriver());
WebElement element = this.config.getDriver().findElement(By.partialLinkText("About Baeldung."));
builder.moveToElement(element).build().perform();
} }
public boolean isAuthorInformationAvailable() { public boolean isAuthorInformationAvailable() {
return webDriver.findElement(By.xpath("//*[contains(text(), 'an engineer with a passion for teaching and building stuff on the web')]")).isDisplayed(); return this.config.getDriver()
.findElement(By.cssSelector("article > .row > div"))
.isDisplayed();
} }
} }

View File

@ -4,9 +4,11 @@ import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.File; import java.io.File;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -15,30 +17,13 @@ public class SeleniumConfig {
private WebDriver driver; private WebDriver driver;
public SeleniumConfig() { public SeleniumConfig() {
Capabilities capabilities = DesiredCapabilities.chrome(); Capabilities capabilities = DesiredCapabilities.firefox();
driver = new ChromeDriver(capabilities); driver = new FirefoxDriver(capabilities);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
} }
static { static {
String osName = getOsName("os.name").toLowerCase(); System.setProperty("webdriver.gecko.driver", findFile("geckodriver.mac"));
final Matcher matcher = Pattern.compile("(mac|nux|win)").matcher(osName);
if (matcher.find()) {
switch (matcher.group()) {
case "mac":
System.setProperty("webdriver.chrome.driver", findFile("chromedriver.mac"));
break;
case "nux":
System.setProperty("webdriver.chrome.driver", findFile("chromedriver.linux"));
break;
case "win":
System.setProperty("webdriver.chrome.driver", findFile("chromedriver.exe"));
break;
}
}
}
private static String getOsName(String prop) {
return (System.getProperty(prop));
} }
static private String findFile(String filename) { static private String findFile(String filename) {
@ -51,7 +36,7 @@ public class SeleniumConfig {
} }
public void close() { public void close() {
driver.quit(); driver.close();
} }
public void navigateTo(String url) { public void navigateTo(String url) {

View File

@ -10,7 +10,7 @@ import static org.testng.Assert.*;
public class SeleniumWithJUnitLiveTest { public class SeleniumWithJUnitLiveTest {
private static SeleniumExample seleniumExample; private static SeleniumExample seleniumExample;
private String expectedTitle = "About Baeldung | Baeldung"; private String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials";
@BeforeClass @BeforeClass
public static void setUp() { public static void setUp() {

View File

@ -10,7 +10,7 @@ import static org.testng.Assert.*;
public class SeleniumWithTestNGLiveTest { public class SeleniumWithTestNGLiveTest {
private SeleniumExample seleniumExample; private SeleniumExample seleniumExample;
private String expectedTitle = "About Baeldung | Baeldung"; private String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials";
@BeforeSuite @BeforeSuite
public void setUp() { public void setUp() {