Skip to content

Commit 1fd0033

Browse files
Add example for asserting with java on page getting_started/using_selenium/
Example for Asserting added for java language, by creating AssertionsTest.java file and adding relevant code lines reference in the markdown file.
1 parent fb58d76 commit 1fd0033

File tree

2 files changed

+61
-14
lines changed

2 files changed

+61
-14
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package dev.selenium.getting_started;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
import org.openqa.selenium.By;
8+
import org.openqa.selenium.WebDriver;
9+
import org.openqa.selenium.chrome.ChromeDriver;
10+
11+
public class AssertionsTest {
12+
13+
@Test
14+
public void assertionTest() {
15+
WebDriver driver = new ChromeDriver();
16+
driver.manage().window().maximize();
17+
18+
driver.get("https://www.selenium.dev/documentation/webdriver/getting_started/using_selenium/");
19+
20+
String expectedUrl = "https://www.selenium.dev/documentation/webdriver/getting_started/using_selenium/";
21+
String actualUrl = driver.getCurrentUrl();
22+
assertEquals(expectedUrl, actualUrl);
23+
24+
String expectedTitle = "Organizing and Executing Selenium Code | Selenium";
25+
String actualTitle = driver.getTitle();
26+
assertEquals(expectedTitle, actualTitle);
27+
28+
boolean isLinkDisplayed = driver.findElement(By.xpath("//a/span[text()='Using Selenium']")).isDisplayed();
29+
assertTrue(isLinkDisplayed);
30+
31+
driver.quit();
32+
}
33+
34+
}

website_and_docs/content/documentation/webdriver/getting_started/using_selenium.en.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ title: "Organizing and Executing Selenium Code"
33
linkTitle: "Using Selenium"
44
weight: 10
55
description: >
6-
Scaling Selenium execution with an IDE and a Test Runner library
6+
Scaling Selenium execution with an IDE and a Test Runner library
77
---
88

99
{{< alert-content >}}
1010
This page is very incomplete and has placeholders for things that need to be added or expounded on.
1111
{{< /alert-content >}}
1212

13-
If you want to run more than a handful of one-off scripts, you need to
13+
If you want to run more than a handful of one-off scripts, you need to
1414
be able to organize and work with your code. This page should give you
1515
ideas for how to actually do productive things with your Selenium code.
1616

1717
## Common Uses
1818

19-
Most people use Selenium to execute automated tests for web applications,
19+
Most people use Selenium to execute automated tests for web applications,
2020
but Selenium supports any use case of browser automation.
2121

2222
### Repetitive Tasks
@@ -36,10 +36,9 @@ Running Selenium for testing requires making assertions on actions taken by Sele
3636
So a good assertion library is required. Additional features to provide structure for tests
3737
require use of [Test Runner](#test-runners).
3838

39-
4039
## IDEs
4140

42-
Regardless of how you use Selenium code,
41+
Regardless of how you use Selenium code,
4342
you won't be very effective writing or executing it without a good
4443
Integrated Developer Environment. Here are some common options...
4544

@@ -54,10 +53,11 @@ Integrated Developer Environment. Here are some common options...
5453
## Test Runner
5554

5655
Even if you aren't using Selenium for testing, if you have advanced use cases, it might make
57-
sense to use a test runner to better organize your code. Being able to use before/after hooks
56+
sense to use a test runner to better organize your code. Being able to use before/after hooks
5857
and run things in groups or in parallel can be very useful.
5958

6059
### Choosing
60+
6161
There are many different test runners available.
6262

6363
All the code examples in this documentation can be found in (or is being moved to) our
@@ -67,36 +67,40 @@ that will be used for all examples on this page.
6767

6868
{{< tabpane text=true >}}
6969
{{% tab header="Java" %}}
70+
7071
- [JUnit](https://junit.org/junit5/) - A widely-used testing framework for Java-based Selenium tests.
7172
- [TestNG](https://testng.org/) - Offers extra features like parallel test execution and parameterized tests.
72-
{{% /tab %}}
73+
{{% /tab %}}
7374

7475
{{% tab header="Python" %}}
76+
7577
- [pytest](https://pytest.org/) - A preferred choice for many, thanks to its simplicity and powerful plugins.
7678
- [unittest](https://docs.python.org/3/library/unittest.html) - Python's standard library testing framework.
77-
{{% /tab %}}
79+
{{% /tab %}}
7880

7981
{{% tab header="CSharp" %}}
82+
8083
- [NUnit](https://nunit.org/) - A popular unit-testing framework for .NET.
8184
- [MS Test](https://docs.microsoft.com/en-us/visualstudio/test/getting-started-with-unit-testing?view=vs-2019) - Microsoft's own unit testing framework.
82-
{{% /tab %}}
85+
{{% /tab %}}
8386

8487
{{% tab header="Ruby" %}}
88+
8589
- [RSpec](https://rspec.info/) - The most widely used testing library for running Selenium tests in Ruby.
8690
- [Minitest](https://github.com/seattlerb/minitest) - A lightweight testing framework that comes with Ruby standard library.
87-
{{% /tab %}}
91+
{{% /tab %}}
8892

8993
{{% tab header="JavaScript" %}}
94+
9095
- [Jest](https://jestjs.io/) - Primarily known as a testing framework for React, it can also be used for Selenium tests.
9196
- [Mocha](https://mochajs.org/) - The most common JS library for running Selenium tests.
92-
{{% /tab %}}
97+
{{% /tab %}}
9398

9499
{{% tab header="Kotlin" %}}
95100

96101
{{% /tab %}}
97102
{{< /tabpane >}}
98103

99-
100104
### Installing
101105

102106
This is very similar to what was required in [Install a Selenium Library]({{< ref "install_library.md" >}}).
@@ -134,9 +138,12 @@ In your project's `package.json`, add requirement to `dependencies`:
134138

135139
### Asserting
136140

141+
Assertions are very important part of Selenium, as they determine whether the state
142+
of the application is same what we are expecting or not. If the assertion fails,
143+
then the test case is failed and execution is stopped.
137144
{{< tabpane text=true >}}
138145
{{< tab header="Java" >}}
139-
{{< badge-code >}}
146+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/getting_started/AssertionsTest.java#L20-L29" >}}
140147
{{< /tab >}}
141148
{{% tab header="Python" %}}
142149
{{< badge-code >}}
@@ -168,10 +175,13 @@ In your project's `package.json`, add requirement to `dependencies`:
168175
{{< badge-code >}}
169176
{{< /tab >}}
170177
{{% tab header="Ruby" %}}
178+
171179
### Set Up
180+
172181
{{< gh-codeblock path="examples/ruby/spec/getting_started/using_selenium_spec.rb#L7-L9" >}}
173182

174183
### Tear Down
184+
175185
{{< gh-codeblock path="examples/ruby/spec/spec_helper.rb#L28" >}}
176186
{{% /tab %}}
177187
{{< tab header="JavaScript" >}}
@@ -186,6 +196,7 @@ In your project's `package.json`, add requirement to `dependencies`:
186196

187197
{{< tabpane text=true >}}
188198
{{% tab header="Java" %}}
199+
189200
### Maven
190201

191202
```shell
@@ -209,9 +220,11 @@ gradle clean test
209220
{{< gh-codeblock path="examples/ruby/README.md#L26" >}}
210221
{{% /tab %}}
211222
{{% tab header="JavaScript" %}}
223+
212224
```shell
213225
mocha runningTests.spec.js
214226
```
227+
215228
{{% /tab %}}
216229
{{< tab header="Kotlin" >}}
217230
{{< badge-code >}}
@@ -246,7 +259,7 @@ Here's an example of that code using a test runner:
246259

247260
## Next Steps
248261

249-
Take what you've learned and build out your Selenium code!
262+
Take what you've learned and build out your Selenium code!
250263

251264
As you find more functionality that you need, read up on the rest of our
252265
[WebDriver documentation]({{< ref "/documentation/webdriver/" >}}).

0 commit comments

Comments
 (0)