Skip to content
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

Expand documentation on fresh browser per test #1671

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,59 @@ If spinning up a new virtual machine is not practical,
at least start a new WebDriver for each test.
Most browser drivers like GeckoDriver and ChromeDriver will start with a clean
known state with a new user profile, by default.

A new browser per test can be achieved by using a test framework's "before each test" hook or fixture. This also implies using the "after each test" hook to close the browser.

```java
// Using a class variable
public abstract class BaseTest {
protected WebDriver driver;
...

// Before each test hook
public void setupTest() {
driver = new FirefoxDriver();
...
}

// After each test hook
public void teardownTest() {
...
driver.quit();
}
}
```

```python
# Using python fixtures
@pytest.fixture(autouse=True, scope='function')
def driver(self, request, page: Page):
# Create the driver
driver = webdriver.Firefox()

# Return control to the test
yield self.driver

# Test ends driver quits
driver.quit()
```
Comment on lines +42 to +52
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


A static WebDriver will cause multiple issues both with parallel test execution but also with keeping alignment with this approach of one browser per test. Aditionally this forces the code to deal with ThreadLocal type techniques that unneccesarily complicate the code.

```java
WebDriver driver = new FirefoxDriver();
# Using a static variable

# This forces the ThreadLocal<WebDriver> variable to call driver.get() every time the driver wants to be used.

# In general static variables in non-thread safe code can have unintended consequences and increase the maintanance effort in the code base.

public abstract class BaseTest {
protected ThreadLocal<WebDriver> driver;
...
// Before each test hook
public void setupTest() {
BaseTest.driver = ThreadLocal.withInitial(()->new FirefoxDriver());
...
}
}
Comment on lines +59 to +71
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,55 @@ aliases: [
Firefoxの場合、既知のプロファイルでWebDriverを起動します。
Most browser drivers like GeckoDriver and ChromeDriver will start with a clean
known state with a new user profile, by default.

```java
// Using a class variable
public abstract class BaseTest {
protected WebDriver driver;
...

// Before each test hook
public void setupTest() {
driver = new FirefoxDriver();
...
}

// After each test hook
public void teardownTest() {
...
driver.quit();
}
}
```

```python
# Using python fixtures
@pytest.fixture(autouse=True, scope='function')
def driver(self, request, page: Page):
# Create the driver
driver = webdriver.Firefox()

# Return control to the test
yield self.driver

# Test ends driver quits
driver.quit()
```

```java
WebDriver driver = new FirefoxDriver();
# Using a static variable

# This forces the ThreadLocal<WebDriver> variable to call driver.get() every time the driver wants to be used.

# In general static variables in non-thread safe code can have unintended consequences and increase the maintanance effort in the code base.

public abstract class BaseTest {
protected ThreadLocal<WebDriver> driver;
...
// Before each test hook
public void setupTest() {
BaseTest.driver = ThreadLocal.withInitial(()->new FirefoxDriver());
...
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,53 @@ pelo menos inicie um novo WebDriver para cada teste.
Most browser drivers like GeckoDriver and ChromeDriver will start with a clean
known state with a new user profile, by default.
```java
WebDriver driver = new FirefoxDriver();
// Using a class variable
public abstract class BaseTest {
protected WebDriver driver;
...

// Before each test hook
public void setupTest() {
driver = new FirefoxDriver();
...
}

// After each test hook
public void teardownTest() {
...
driver.quit();
}
}
```

```python
# Using python fixtures
@pytest.fixture(autouse=True, scope='function')
def driver(self, request, page: Page):
# Create the driver
driver = webdriver.Firefox()

# Return control to the test
yield self.driver

# Test ends driver quits
driver.quit()
```

```java
# Using a static variable

# This forces the ThreadLocal<WebDriver> variable to call driver.get() every time the driver wants to be used.

# In general static variables in non-thread safe code can have unintended consequences and increase the maintanance effort in the code base.

public abstract class BaseTest {
protected ThreadLocal<WebDriver> driver;
...
// Before each test hook
public void setupTest() {
BaseTest.driver = ThreadLocal.withInitial(()->new FirefoxDriver());
...
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,53 @@ aliases: [
对于Firefox, 请使用您已知的配置文件去启动WebDriver.
大多数浏览器驱动器,像GeckoDriver和ChromeDriver那样,默认都会以干净的已知状态和一个新的用户配置文件开始。
```java
WebDriver driver = new FirefoxDriver();
// Using a class variable
public abstract class BaseTest {
protected WebDriver driver;
...

// Before each test hook
public void setupTest() {
driver = new FirefoxDriver();
...
}

// After each test hook
public void teardownTest() {
...
driver.quit();
}
}
```

```python
# Using python fixtures
@pytest.fixture(autouse=True, scope='function')
def driver(self, request, page: Page):
# Create the driver
driver = webdriver.Firefox()

# Return control to the test
yield self.driver

# Test ends driver quits
driver.quit()
```

```java
# Using a static variable

# This forces the ThreadLocal<WebDriver> variable to call driver.get() every time the driver wants to be used.

# In general static variables in non-thread safe code can have unintended consequences and increase the maintanance effort in the code base.

public abstract class BaseTest {
protected ThreadLocal<WebDriver> driver;
...
// Before each test hook
public void setupTest() {
BaseTest.driver = ThreadLocal.withInitial(()->new FirefoxDriver());
...
}
}
```