java-20085: fix selenium failing tests (#14279)

This commit is contained in:
Ehsan Sasanianno 2023-06-22 19:30:27 +02:00 committed by GitHub
parent a6052bde93
commit 6ac079cc26
5 changed files with 29 additions and 33 deletions

View File

@ -10,7 +10,7 @@ public class BaeldungHomePage {
private SeleniumConfig config;
@FindBy(css = ".nav--logo_mobile")
private WebElement title;
@FindBy(css = ".menu-start-here > a")
@FindBy(css = ".header--menu")
private WebElement startHere;
public BaeldungHomePage(SeleniumConfig config) {

View File

@ -39,22 +39,21 @@ public class SeleniumJavaScriptClickLiveTest {
public void whenSearchForSeleniumArticles_thenReturnNotEmptyResults() {
driver.get("https://baeldung.com");
String title = driver.getTitle();
assertEquals("Baeldung | Java, Spring and Web Development tutorials", title);
assertEquals("Baeldung", title);
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.id("menu-item-40489")));
WebElement searchInput = driver.findElement(By.id("menu-item-40489"));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".btn-search")));
WebElement seeSearchResultsButton = driver.findElement(By.cssSelector(".btn-search"));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".nav--menu_item")));
WebElement seeSearchResultsButton = driver.findElement(By.cssSelector(".nav--menu_item"));
clickElement(seeSearchResultsButton);
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("post")));
int seleniumPostsCount = driver.findElements(By.className("post"))
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("nav--menu_item_anchor")));
int seleniumPostsCount = driver.findElements(By.className("nav--menu_item_anchor"))
.size();
assertTrue(seleniumPostsCount > 0);
}

View File

@ -19,19 +19,24 @@ import org.junit.Test;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SeleniumCookiesJUnitLiveTest {
private WebDriver driver;
private String navUrl;
private final String COOKIE = "SNS";
@Before
public void setUp() {
System.setProperty("webdriver.gecko.driver", findFile("geckodriver.mac"));
driver = new FirefoxDriver();
navUrl = "https://baeldung.com";
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.navigate().to(navUrl);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(1000));
wait.until(d -> d.manage().getCookieNamed(COOKIE) != null);
}
private static String findFile(String filename) {
@ -50,7 +55,6 @@ public class SeleniumCookiesJUnitLiveTest {
@Test
public void whenNavigate_thenCookiesExist() {
driver.navigate().to(navUrl);
Set<Cookie> cookies = driver.manage().getCookies();
assertThat(cookies, is(not(empty())));
@ -58,35 +62,30 @@ public class SeleniumCookiesJUnitLiveTest {
@Test
public void whenNavigate_thenLpCookieExists() {
driver.navigate().to(navUrl);
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
Cookie lpCookie = driver.manage().getCookieNamed(COOKIE);
assertThat(lpCookie, is(not(nullValue())));
}
@Test
public void whenNavigate_thenLpCookieIsHasCorrectValue() {
driver.navigate().to(navUrl);
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
Cookie lpCookie = driver.manage().getCookieNamed(COOKIE);
assertThat(lpCookie.getValue(), containsString("www.baeldung.com"));
assertThat(lpCookie.getValue(), containsString("1"));
}
@Test
public void whenNavigate_thenLpCookieHasCorrectProps() {
driver.navigate().to(navUrl);
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
Cookie lpCookie = driver.manage().getCookieNamed(COOKIE);
assertThat(lpCookie.getDomain(), equalTo(".baeldung.com"));
assertThat(lpCookie.getDomain(), equalTo("www.baeldung.com"));
assertThat(lpCookie.getPath(), equalTo("/"));
assertThat(lpCookie.getExpiry(), is(not(nullValue())));
assertThat(lpCookie.isSecure(), equalTo(false));
assertThat(lpCookie.isHttpOnly(), equalTo(false));
}
@Test
public void whenAddingCookie_thenItIsPresent() {
driver.navigate().to(navUrl);
Cookie cookie = new Cookie("foo", "bar");
driver.manage().addCookie(cookie);
Cookie driverCookie = driver.manage().getCookieNamed("foo");
@ -96,27 +95,25 @@ public class SeleniumCookiesJUnitLiveTest {
@Test
public void whenDeletingCookie_thenItIsAbsent() {
driver.navigate().to(navUrl);
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
Cookie lpCookie = driver.manage().getCookieNamed("SNS");
assertThat(lpCookie, is(not(nullValue())));
driver.manage().deleteCookie(lpCookie);
Cookie deletedCookie = driver.manage().getCookieNamed("lp_120073");
Cookie deletedCookie = driver.manage().getCookieNamed(COOKIE);
assertThat(deletedCookie, is(nullValue()));
}
@Test
public void whenOverridingCookie_thenItIsUpdated() {
driver.navigate().to(navUrl);
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
Cookie lpCookie = driver.manage().getCookieNamed(COOKIE);
driver.manage().deleteCookie(lpCookie);
Cookie newLpCookie = new Cookie("lp_120073", "foo");
Cookie newLpCookie = new Cookie(COOKIE, "foo");
driver.manage().addCookie(newLpCookie);
Cookie overriddenCookie = driver.manage().getCookieNamed("lp_120073");
Cookie overriddenCookie = driver.manage().getCookieNamed(COOKIE);
assertThat(overriddenCookie.getValue(), equalTo("foo"));
}

View File

@ -13,9 +13,9 @@ final class InvalidSetupLiveTest {
@BeforeAll
static void setup() {
// Make sure the properties are cleared before the tests.
System.clearProperty("webdriver.chrome.driver");
System.clearProperty("webdriver.gecko.driver");
System.clearProperty("webdriver.edge.driver");
System.setProperty("webdriver.chrome.driver", "");
System.setProperty("webdriver.gecko.driver","");
System.setProperty("webdriver.edge.driver","");
}
@Test

View File

@ -16,7 +16,7 @@ public class SeleniumWebDriverUnitTest {
private WebDriver driver;
private static final String URL = "https://duckduckgo.com/";
private static final String INPUT_ID = "search_form_input_homepage";
private static final String INPUT_ID = "searchbox_input__bEGm3";
@BeforeEach
public void setUp() {
@ -32,7 +32,7 @@ public class SeleniumWebDriverUnitTest {
@Test
public void givenDuckDuckGoHomePage_whenInputHelloWorld_thenInputValueIsHelloWorld() {
driver.get(URL);
WebElement inputElement = driver.findElement(By.id(INPUT_ID));
WebElement inputElement = driver.findElement(By.className(INPUT_ID));
inputElement.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.DELETE);
inputElement.sendKeys("Hello World!");