-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add example for asserting with java on getting_started/using_selenium #1672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diemol
merged 5 commits into
SeleniumHQ:trunk
from
harshitBhardwaj97:feat/add-examples-getting-started-using-selenium
Apr 16, 2024
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1fd0033
Add example for asserting with java on page getting_started/using_sel…
harshitBhardwaj97 9f9d814
Documentation updated for using_selenium.ja.md
harshitBhardwaj97 fd827c0
Add example of Setting up and Tear down with java
harshitBhardwaj97 158ba8c
Assertion and Setup/Teardown examples linked
harshitBhardwaj97 340ef4d
Do the requested changes
harshitBhardwaj97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
examples/java/src/test/java/dev/selenium/getting_started/SetupAndTeardownTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package dev.selenium.getting_started; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.time.Duration; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.chrome.ChromeDriver; | ||
import org.openqa.selenium.support.ui.ExpectedConditions; | ||
import org.openqa.selenium.support.ui.WebDriverWait; | ||
|
||
public class SetupAndTeardownTest { | ||
|
||
private WebDriver driver; | ||
private WebDriverWait wait; | ||
private By projectsLink = By.xpath("//a/span[text()='Projects']"); | ||
private By searchFieldSpan = By.xpath("//span[text()='Search']"); | ||
private By searchField = By.id("docsearch-input"); | ||
private By searchResultsFirstItem = By.cssSelector("ul[role='listbox'] li"); | ||
|
||
@BeforeAll | ||
public static void beforeAll() { | ||
System.out.println("This method will be executed only once before all the tests."); | ||
} | ||
|
||
@BeforeEach | ||
public void setup() { | ||
System.out.println( | ||
" This method will be executed before each test, so here write the common code that has to be executed."); | ||
driver = new ChromeDriver(); | ||
driver.manage().window().maximize(); | ||
driver.get("https://www.selenium.dev/documentation/webdriver/getting_started/using_selenium/"); | ||
} | ||
|
||
@Test | ||
public void projectsAreDisplayed_whenNavigatedToProjectsPage() { | ||
driver.findElement(projectsLink).click(); | ||
String expectedUrl = "https://www.selenium.dev/projects/"; | ||
String actualUrl = driver.getCurrentUrl(); | ||
assertEquals(expectedUrl, actualUrl); | ||
} | ||
|
||
@Test | ||
public void searchFieldTest() { | ||
driver.findElement(searchFieldSpan).click(); | ||
driver.findElement(searchField).sendKeys("Test Practices"); | ||
|
||
wait = new WebDriverWait(driver, Duration.ofSeconds(5)); | ||
wait.until(ExpectedConditions.presenceOfElementLocated(searchResultsFirstItem)); | ||
|
||
driver.findElement(searchResultsFirstItem).click(); | ||
|
||
String expectedUrl = "https://www.selenium.dev/documentation/test_practices/"; | ||
String actualUrl = driver.getCurrentUrl(); | ||
assertEquals(expectedUrl, actualUrl); | ||
|
||
assertTrue(driver.findElement(By.xpath("//h1[.='Test Practices']")).isDisplayed()); | ||
} | ||
|
||
@AfterEach | ||
public void teardown() { | ||
System.out.println(" This method will be executed after each test, so here write the clean up code."); | ||
driver.quit(); | ||
} | ||
|
||
@AfterAll | ||
public static void afterAll() { | ||
System.out.println("This method will be executed only once after all the tests."); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.