Skip to content

Commit 9cf5336

Browse files
Walter Gómezpivovarit
authored andcommitted
Fix/update selenium config (eugenp#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
1 parent 3a04c3a commit 9cf5336

9 files changed

Lines changed: 25 additions & 37 deletions

File tree

-7.41 MB
Binary file not shown.
-7.15 MB
Binary file not shown.
-10.4 MB
Binary file not shown.
3.61 MB
Binary file not shown.

‎selenium-junit-testng/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@
6666

6767
<properties>
6868
<testng.version>6.10</testng.version>
69-
<selenium-java.version>3.0.1</selenium-java.version>
69+
<selenium-java.version>3.4.0</selenium-java.version>
7070
</properties>
7171
</project>
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
package main.java.com.baeldung.selenium;
22

3+
import main.java.com.baeldung.selenium.config.SeleniumConfig;
34
import org.openqa.selenium.By;
45
import org.openqa.selenium.WebDriver;
56
import org.openqa.selenium.WebElement;
67
import org.openqa.selenium.firefox.FirefoxDriver;
8+
import org.openqa.selenium.interactions.Actions;
79

810
import java.util.List;
911
import java.util.concurrent.TimeUnit;
1012

1113
public class SeleniumExample {
1214

13-
private WebDriver webDriver;
15+
private SeleniumConfig config;
1416
private String url = "http://www.baeldung.com/";
1517

1618
public SeleniumExample() {
17-
System.setProperty("webdriver.firefox.marionette", "C:\\selenium\\geckodriver.exe");
18-
webDriver = new FirefoxDriver();
19-
webDriver.manage().window().maximize();
20-
webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
21-
webDriver.get(url);
19+
config = new SeleniumConfig();
20+
config.getDriver().get(url);
2221
}
2322

2423
public void closeWindow() {
25-
webDriver.close();
24+
this.config.getDriver().close();
2625
}
2726

2827
public String getTitle() {
29-
return webDriver.getTitle();
28+
return this.config.getDriver().getTitle();
3029
}
3130

3231
public void getAboutBaeldungPage() {
@@ -36,7 +35,7 @@ public void getAboutBaeldungPage() {
3635
}
3736

3837
private void closeOverlay() {
39-
List<WebElement> webElementList = webDriver.findElements(By.tagName("a"));
38+
List<WebElement> webElementList = this.config.getDriver().findElements(By.tagName("a"));
4039
if (webElementList != null) {
4140
webElementList.stream()
4241
.filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title")))
@@ -47,14 +46,18 @@ private void closeOverlay() {
4746
}
4847

4948
private void clickAboutLink() {
50-
webDriver.findElement(By.partialLinkText("About")).click();
49+
this.config.getDriver().findElement(By.partialLinkText("About")).click();
5150
}
5251

5352
private void clickAboutUsLink() {
54-
webDriver.findElement(By.partialLinkText("About Baeldung.")).click();
53+
Actions builder = new Actions(config.getDriver());
54+
WebElement element = this.config.getDriver().findElement(By.partialLinkText("About Baeldung."));
55+
builder.moveToElement(element).build().perform();
5556
}
5657

5758
public boolean isAuthorInformationAvailable() {
58-
return webDriver.findElement(By.xpath("//*[contains(text(), 'an engineer with a passion for teaching and building stuff on the web')]")).isDisplayed();
59+
return this.config.getDriver()
60+
.findElement(By.cssSelector("article > .row > div"))
61+
.isDisplayed();
5962
}
6063
}

‎selenium-junit-testng/src/main/java/com/baeldung/selenium/config/SeleniumConfig.java‎

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import org.openqa.selenium.WebDriver;
55
import org.openqa.selenium.WebElement;
66
import org.openqa.selenium.chrome.ChromeDriver;
7+
import org.openqa.selenium.firefox.FirefoxDriver;
78
import org.openqa.selenium.remote.DesiredCapabilities;
89

910
import java.io.File;
11+
import java.util.concurrent.TimeUnit;
1012
import java.util.regex.Matcher;
1113
import java.util.regex.Pattern;
1214

@@ -15,30 +17,13 @@ public class SeleniumConfig {
1517
private WebDriver driver;
1618

1719
public SeleniumConfig() {
18-
Capabilities capabilities = DesiredCapabilities.chrome();
19-
driver = new ChromeDriver(capabilities);
20+
Capabilities capabilities = DesiredCapabilities.firefox();
21+
driver = new FirefoxDriver(capabilities);
22+
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
2023
}
2124

2225
static {
23-
String osName = getOsName("os.name").toLowerCase();
24-
final Matcher matcher = Pattern.compile("(mac|nux|win)").matcher(osName);
25-
if (matcher.find()) {
26-
switch (matcher.group()) {
27-
case "mac":
28-
System.setProperty("webdriver.chrome.driver", findFile("chromedriver.mac"));
29-
break;
30-
case "nux":
31-
System.setProperty("webdriver.chrome.driver", findFile("chromedriver.linux"));
32-
break;
33-
case "win":
34-
System.setProperty("webdriver.chrome.driver", findFile("chromedriver.exe"));
35-
break;
36-
}
37-
}
38-
}
39-
40-
private static String getOsName(String prop) {
41-
return (System.getProperty(prop));
26+
System.setProperty("webdriver.gecko.driver", findFile("geckodriver.mac"));
4227
}
4328

4429
static private String findFile(String filename) {
@@ -51,7 +36,7 @@ static private String findFile(String filename) {
5136
}
5237

5338
public void close() {
54-
driver.quit();
39+
driver.close();
5540
}
5641

5742
public void navigateTo(String url) {

‎selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumWithJUnitLiveTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class SeleniumWithJUnitLiveTest {
1111

1212
private static SeleniumExample seleniumExample;
13-
private String expectedTitle = "About Baeldung | Baeldung";
13+
private String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials";
1414

1515
@BeforeClass
1616
public static void setUp() {

‎selenium-junit-testng/src/test/java/com/baeldung/selenium/testng/SeleniumWithTestNGLiveTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class SeleniumWithTestNGLiveTest {
1111

1212
private SeleniumExample seleniumExample;
13-
private String expectedTitle = "About Baeldung | Baeldung";
13+
private String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials";
1414

1515
@BeforeSuite
1616
public void setUp() {

0 commit comments

Comments
 (0)