Skip to content

Commit

Permalink
Merge branch 'trunk' into update-python-browsers-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
harsha509 authored Aug 20, 2024
2 parents f730fe6 + 65c0f38 commit 247ac2e
Show file tree
Hide file tree
Showing 50 changed files with 1,887 additions and 277 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/java-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
command: |
pip install yq
xml_content=$(curl -sf https://oss.sonatype.org/service/local/repositories/snapshots/content/org/seleniumhq/selenium/selenium-java/)
latest_snapshot=$(echo $xml_content | xq '.content.data."content-item"' | jq -r 'sort_by(.lastModified) | reverse | .[0] | .text')
latest_snapshot=$(echo $xml_content | xq '.content.data."content-item"' | jq -r .text)
echo $latest_snapshot
cd examples/java
mvn -B -U test -Dselenium.version="$latest_snapshot"
Expand All @@ -81,7 +81,7 @@ jobs:
command: |
pip install yq
$xml_content = Invoke-WebRequest -Uri "https://oss.sonatype.org/service/local/repositories/snapshots/content/org/seleniumhq/selenium/selenium-java/"
$latest_snapshot = $xml_content.Content | xq '.content.data.\"content-item\"' | jq -r 'sort_by(.lastModified) | reverse | .[0] | .text'
$latest_snapshot = $xml_content.Content | xq '.content.data.\"content-item\"' | jq -r .text
Write-Output $latest_snapshot
cd examples/java
mvn -B -U test "-Dselenium.version=$latest_snapshot"
2 changes: 1 addition & 1 deletion examples/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.1</version>
<version>3.4.0</version>
<configuration>
<properties>
<configurationParameters>
Expand Down
109 changes: 109 additions & 0 deletions examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package dev.selenium.drivers;

import dev.selenium.BaseTest;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.chrome.ChromeDriver;

public class OptionsTest extends BaseTest {
Expand Down Expand Up @@ -60,5 +67,107 @@ public void setAcceptInsecureCerts() {
driver.quit();
}
}

@Test
public void getBrowserName() {
ChromeOptions chromeOptions = new ChromeOptions();
String name = chromeOptions.getBrowserName();
Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");
}

@Test
public void setBrowserVersion() {
ChromeOptions chromeOptions = new ChromeOptions();
String version = "latest";
chromeOptions.setBrowserVersion(version);
Assertions.assertEquals(version, chromeOptions.getBrowserVersion());
}

@Test
public void setPlatformName() {
ChromeOptions chromeOptions = new ChromeOptions();
String platform = "OS X 10.6";
chromeOptions.setPlatformName(platform);
Assertions.assertEquals(platform, chromeOptions.getPlatformName().toString());
}

@Test
public void setScriptTimeout() {
ChromeOptions chromeOptions = new ChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setScriptTimeout(duration);

WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getScriptTimeout();
Assertions.assertEquals(timeout, duration, "The script timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}

@Test
public void setPageLoadTimeout() {
ChromeOptions chromeOptions = new ChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setPageLoadTimeout(duration);

WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
Assertions.assertEquals(timeout, duration, "The page load timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}

@Test
public void setImplicitWaitTimeout() {
ChromeOptions chromeOptions = new ChromeOptions();
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
chromeOptions.setImplicitWaitTimeout(duration);

WebDriver driver = new ChromeDriver(chromeOptions);
try {
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
Assertions.assertEquals(timeout, duration, "The implicit wait timeout should be set to 5 seconds.");
} finally {
driver.quit();
}
}

@Test
public void setUnhandledPromptBehaviour() {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);
Assertions.assertNotNull(capabilityObject, "Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");
Assertions.assertEquals(capabilityObject.toString(), UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());
}

@Test
public void setWindowRect() {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);
Assertions.assertNotNull(capabilityObject, "Capability SET_WINDOW_RECT should not be null.");

Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability SET_WINDOW_RECT should be set to true.");
}

@Test
public void setStrictFileInteractability() {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
//verify the capability object is not null
Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");

Boolean capability = (Boolean) capabilityObject;
Assertions.assertTrue(capability, "The capability STRICT_FILE_INTERACTABILITY should be set to true.");
}
}

2 changes: 1 addition & 1 deletion examples/kotlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<junit5.version>5.11.0</junit5.version>
<wdm.version>5.2.3</wdm.version>

<maven-surefire-plugin.version>3.3.1</maven-surefire-plugin.version>
<maven-surefire-plugin.version>3.4.0</maven-surefire-plugin.version>

<java.version>1.8</java.version>
<selenium.version>4.23.1</selenium.version>
Expand Down
8 changes: 8 additions & 0 deletions examples/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ pytest
```

> Please keep some patience - If you are doing it for the first time, it will take a little while to verify and download the browser drivers
## Execute a specific example
To run a specific Selenium Python example, use the following command:
```bash
python first_script.py
```

Make sure to replace `first_script.py` with the path and name of the example you want to run.
8 changes: 8 additions & 0 deletions examples/python/tests/getting_started/using_selenium_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ def test_eight_components():
assert value == "Received!"

driver.quit()

def setup():
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
return driver

def teardown(driver):
driver.quit()
12 changes: 12 additions & 0 deletions examples/python/tests/selenium_manager/usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

def setup_without_selenium_manager():
chrome_service = Service(executable_path='path/to/chrome.exe')
driver = webdriver.Chrome(chrome_service)
return driver

def setup_with_selenium_manager():
driver = webdriver.Chrome()
return driver
2 changes: 1 addition & 1 deletion examples/ruby/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ gem 'rspec', '~> 3.0'
gem 'rubocop', '~> 1.35'
gem 'rubocop-rspec', '~> 3.0'
gem 'selenium-devtools', '= 0.127.0'
gem 'selenium-webdriver', '= 4.23.0'
gem 'selenium-webdriver', '= 4.23.0'
1 change: 1 addition & 0 deletions examples/ruby/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ GEM
PLATFORMS
arm64-darwin-21
arm64-darwin-22
arm64-darwin-23
x86_64-darwin-19
x86_64-darwin-20
x86_64-darwin-22
Expand Down
43 changes: 43 additions & 0 deletions examples/ruby/spec/interactions/alerts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,47 @@

RSpec.describe 'Alerts' do
let(:driver) { start_session }

before do
driver.navigate.to 'https://selenium.dev'
end

it 'interacts with an alert' do
driver.execute_script 'alert("Hello, World!")'

# Store the alert reference in a variable
alert = driver.switch_to.alert

# Get the text of the alert
alert.text

# Press on Cancel button
alert.dismiss
end

it 'interacts with a confirm' do
driver.execute_script 'confirm("Are you sure?")'

# Store the alert reference in a variable
alert = driver.switch_to.alert

# Get the text of the alert
alert.text

# Press on Cancel button
alert.dismiss
end

it 'interacts with a prompt' do
driver.execute_script 'prompt("What is your name?")'

# Store the alert reference in a variable
alert = driver.switch_to.alert

# Type a message
alert.send_keys('selenium')

# Press on Ok button
alert.accept
end
end
4 changes: 3 additions & 1 deletion examples/ruby/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
config.after { @driver&.quit }

def start_session
@driver = Selenium::WebDriver.for :chrome
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('disable-search-engine-choice-screen')
@driver = Selenium::WebDriver.for(:chrome, options: options)
end

def start_bidi_session
Expand Down
40 changes: 40 additions & 0 deletions website_and_docs/content/blog/2024/chrome-browser-woes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: "Two Chrome features you should disable"
linkTitle: "Two Chrome features you should disable"
date: 2024-08-18
tags: ["chrome"]
categories: ["general"]
author: Marek Andreansky [@skyhirider](https://www.linkedin.com/in/marekandreansky/)
description: This blog post lists two problematic Chrome features that can affect your automation as well as a quick way to disable them.
---

## Search engine selection screen

Starting with version 127 of Chrome, the browser [now asks which search engine you would like to use](https://www.google.com/chrome/choicescreen/),
which is great for regular users.

But for automation, it does so every single time.

To bypass this, use the argument below when specifying the browser options.

```
--disable-search-engine-choice-screen
```

## Chrome wastes over 60MB of your bandwidth downloading language plugins

The second feature is something that has been with Chrome for quite a while now.

Every time you launch Chrome,
[it will query and download several .crx files](https://www.reddit.com/r/chrome/comments/u78vd0/chrome_has_constantly_been_downloading_something/).

These files can even be left over on your disk's download folder if you create and close drivers faster than these can be processed.

To disable this feature, use the browser option below.

```
--disable-features=OptimizationGuideModelDownloading,OptimizationHintsFetching,OptimizationTargetPrediction,OptimizationHints
```


_This is a guest blog post by [Marek Andreansky](https://www.linkedin.com/in/marekandreansky/)_
Loading

0 comments on commit 247ac2e

Please sign in to comment.