Skip to content

Commit cf913c3

Browse files
Add example for asserting with java on getting_started/using_selenium (#1672)
* 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. * Documentation updated for using_selenium.ja.md * Add example of Setting up and Tear down with java Example for Setting up and Tearing down added for java language, by creating SetupAndTeardownTest.java file, and adding relevant code lines reference in the markdown file(s). * Assertion and Setup/Teardown examples linked Assertion and Setup/Teardown examples linked with existing UsingSeleniumTest.java, removed AssertionsTest.java and SetupAndTeardownTest.java * Do the requested changes 1. BeforeAll and AfterAll functions removed. 2. Print statements removed. 3. driver.manage().window().maximize() removed. 4. Driver navigation and implicit wait code moved into test body. 5. Markdown changes done in all the 4 languages. [deploy site]
1 parent fb58d76 commit cf913c3

File tree

5 files changed

+178
-94
lines changed

5 files changed

+178
-94
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,50 @@
11
package dev.selenium.getting_started;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.time.Duration;
6+
7+
import org.junit.jupiter.api.AfterEach;
8+
import org.junit.jupiter.api.BeforeEach;
39
import org.junit.jupiter.api.Test;
410
import org.openqa.selenium.By;
511
import org.openqa.selenium.WebDriver;
612
import org.openqa.selenium.WebElement;
713
import org.openqa.selenium.chrome.ChromeDriver;
814

9-
import java.time.Duration;
15+
public class UsingSeleniumTest {
1016

11-
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
WebDriver driver;
1218

13-
public class UsingSeleniumTest {
19+
@BeforeEach
20+
public void setup() {
21+
driver = new ChromeDriver();
22+
}
23+
24+
@Test
25+
public void eightComponents() {
1426

15-
@Test
16-
public void eightComponents() {
17-
WebDriver driver = new ChromeDriver();
18-
driver.get("https://www.selenium.dev/selenium/web/web-form.html");
27+
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
28+
driver.get("https://www.selenium.dev/selenium/web/web-form.html");
1929

20-
String title = driver.getTitle();
21-
assertEquals("Web form", title);
30+
String title = driver.getTitle();
31+
assertEquals("Web form", title);
2232

23-
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
33+
WebElement textBox = driver.findElement(By.name("my-text"));
34+
WebElement submitButton = driver.findElement(By.cssSelector("button"));
2435

25-
WebElement textBox = driver.findElement(By.name("my-text"));
26-
WebElement submitButton = driver.findElement(By.cssSelector("button"));
36+
textBox.sendKeys("Selenium");
37+
submitButton.click();
2738

28-
textBox.sendKeys("Selenium");
29-
submitButton.click();
39+
WebElement message = driver.findElement(By.id("message"));
40+
String value = message.getText();
41+
assertEquals("Received!", value);
3042

31-
WebElement message = driver.findElement(By.id("message"));
32-
String value = message.getText();
33-
assertEquals("Received!", value);
43+
}
3444

35-
driver.quit();
36-
}
45+
@AfterEach
46+
public void teardown() {
47+
driver.quit();
48+
}
3749

3850
}

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

+35-17
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" >}}).
@@ -136,7 +140,7 @@ In your project's `package.json`, add requirement to `dependencies`:
136140

137141
{{< tabpane text=true >}}
138142
{{< tab header="Java" >}}
139-
{{< badge-code >}}
143+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/getting_started/UsingSeleniumTest.java#L30-L31" >}}
140144
{{< /tab >}}
141145
{{% tab header="Python" %}}
142146
{{< badge-code >}}
@@ -158,20 +162,31 @@ In your project's `package.json`, add requirement to `dependencies`:
158162
### Setting Up and Tearing Down
159163

160164
{{< tabpane text=true >}}
161-
{{< tab header="Java" >}}
162-
{{< badge-code >}}
163-
{{< /tab >}}
165+
{{% tab header="Java" %}}
166+
167+
### Set Up
168+
169+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/getting_started/UsingSeleniumTest.java#L19-L22" >}}
170+
171+
### Tear Down
172+
173+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/getting_started/UsingSeleniumTest.java#L45-L48" >}}
174+
175+
{{% /tab %}}
164176
{{% tab header="Python" %}}
165177
{{< badge-code >}}
166178
{{% /tab %}}
167179
{{< tab header="CSharp" >}}
168180
{{< badge-code >}}
169181
{{< /tab >}}
170182
{{% tab header="Ruby" %}}
183+
171184
### Set Up
185+
172186
{{< gh-codeblock path="examples/ruby/spec/getting_started/using_selenium_spec.rb#L7-L9" >}}
173187

174188
### Tear Down
189+
175190
{{< gh-codeblock path="examples/ruby/spec/spec_helper.rb#L28" >}}
176191
{{% /tab %}}
177192
{{< tab header="JavaScript" >}}
@@ -186,6 +201,7 @@ In your project's `package.json`, add requirement to `dependencies`:
186201

187202
{{< tabpane text=true >}}
188203
{{% tab header="Java" %}}
204+
189205
### Maven
190206

191207
```shell
@@ -209,9 +225,11 @@ gradle clean test
209225
{{< gh-codeblock path="examples/ruby/README.md#L26" >}}
210226
{{% /tab %}}
211227
{{% tab header="JavaScript" %}}
228+
212229
```shell
213230
mocha runningTests.spec.js
214231
```
232+
215233
{{% /tab %}}
216234
{{< tab header="Kotlin" >}}
217235
{{< badge-code >}}
@@ -246,7 +264,7 @@ Here's an example of that code using a test runner:
246264

247265
## Next Steps
248266

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

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

0 commit comments

Comments
 (0)