Skip to content

Added code samples for Chrome extensions #1411

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
merged 14 commits into from
Jun 27, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.openqa.selenium.chromium.ChromiumDriverLogLevel;

import java.io.File;
Expand Down Expand Up @@ -49,7 +55,6 @@ public void excludeSwitches() {
driver = new ChromeDriver(options);
}

@Test
public void logsToFile() throws IOException {
ChromeDriverService service = new ChromeDriverService.Builder()
.withLogFile(getLogLocation())
Expand Down Expand Up @@ -127,6 +132,17 @@ public void disableBuildChecks() throws IOException {
Assertions.assertTrue(fileContent.contains(expected));
}

@Test
public void extensionOptions() {
ChromeOptions options = new ChromeOptions();
Path path = Paths.get("src/test/resources/extensions/webextensions-selenium-example.crx");
options.addExtensions(new File(path.toUri()));
driver = new ChromeDriver(options);
driver.get("https://www.selenium.dev/selenium/web/blank.html");
WebElement injected = driver.findElement(By.id("webextensions-selenium-example"));
Assertions.assertEquals("Content injected by webextensions-selenium-example", injected.getText());
}

private File getLogLocation() throws IOException {
if (logLocation == null || !logLocation.exists()) {
logLocation = File.createTempFile("chromedriver-", ".log");
Expand Down
Binary file not shown.
11 changes: 11 additions & 0 deletions examples/javascript/test/browser/chromeSpecificCaps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,16 @@ suite(function (env) {
await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});

it('Add Extension', async function () {
const options = new Chrome.Options();
let driver = await env
.builder()
.setChromeOptions(options.addExtensions(['./test/resources/extensions/webextensions-selenium-example.crx']))
.build();

await driver.get('https://www.selenium.dev/selenium/web/blank.html');
await driver.quit();
});
});
}, { browsers: [Browser.CHROME]});
Binary file not shown.
12 changes: 12 additions & 0 deletions examples/python/tests/browsers/test_chrome.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import re
import subprocess

Expand Down Expand Up @@ -98,3 +99,14 @@ def test_build_checks(log_path):

driver.quit()

driver.quit()

def test_add_extension():
chrome_options = webdriver.ChromeOptions()
path = os.path.abspath("tests/extensions/webextensions-selenium-example.crx")
chrome_options.add_extension(path)

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.selenium.dev/selenium/web/blank.html");

driver.quit()
Binary file not shown.
12 changes: 12 additions & 0 deletions examples/ruby/spec/browsers/chrome_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,17 @@
warning = /\[WARNING\]: You are using an unsupported command-line switch: --disable-build-check/
expect(File.readlines(file_name).grep(warning).any?).to eq true
end

it 'Add extensions' do
extension_file_path = File.expand_path('../extensions/webextensions-selenium-example.crx', __dir__)
options = Selenium::WebDriver::Options.chrome
options.add_extension(extension_file_path)

@driver = Selenium::WebDriver.for :chrome, options: options
@driver.get("https://www.selenium.dev/selenium/web/blank.html");
injected = @driver.find_element(:id, 'webextensions-selenium-example')
expect(injected.text).to eq 'Content injected by webextensions-selenium-example'
end

end
end
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,19 @@ Add an extension to options:

{{< tabpane text=true langEqualsHeader=true >}}
{{< tab header="Java" >}}
{{< badge-code >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/browsers/ChromeTest.java#L137-L139" >}}
{{< /tab >}}
{{% tab header="Python" %}}
{{< badge-code >}}
{{< gh-codeblock path="/examples/python/tests/browsers/test_chrome.py#L105-L107">}}
{{% /tab %}}
{{< tab header="CSharp" >}}
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Browsers/ChromeTest.cs#L50" >}}
{{< /tab >}}
{{< tab header="Ruby" >}}
{{< badge-code >}}
{{< gh-codeblock path="/examples/ruby/spec/browsers/chrome_spec.rb#L94-L96" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
{{< badge-code >}}
{{< gh-codeblock path="/examples/javascript/test/browser/chromeSpecificCaps.spec.js#L62-L66">}}
{{< /tab >}}
{{< tab header="Kotlin" >}}
{{< badge-code >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,19 @@ please use the `load-extension` argument instead, as mentioned in

{{< tabpane text=true langEqualsHeader=true >}}
{{< tab header="Java" >}}
{{< badge-code >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/browsers/ChromeTest.java#L44-L47" >}}
{{< /tab >}}
{{% tab header="Python" %}}
{{< badge-code >}}
{{< gh-codeblock path="/examples/python/tests/browsers/test_chrome.py#L41-L43">}}
{{% /tab %}}
{{< tab header="CSharp" >}}
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Browsers/ChromeTest.cs#L50" >}}
{{< /tab >}}
{{< tab header="Ruby" >}}
{{< badge-code >}}
{{< gh-codeblock path="/examples/ruby/spec/browsers/chrome_spec.rb#L35-L37" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
{{< badge-code >}}
{{< gh-codeblock path="/examples/javascript/test/browser/chromeSpecificCaps.spec.js#L51-L55">}}
{{< /tab >}}
{{< tab header="Kotlin" >}}
{{< badge-code >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,19 @@ Adicionar uma extensão:

{{< tabpane text=true langEqualsHeader=true >}}
{{< tab header="Java" >}}
{{< badge-code >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/browsers/ChromeTest.java#L44-L47" >}}
{{< /tab >}}
{{% tab header="Python" %}}
{{< badge-code >}}
{{< gh-codeblock path="/examples/python/tests/browsers/test_chrome.py#L41-L43">}}
{{% /tab %}}
{{< tab header="CSharp" >}}
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Browsers/ChromeTest.cs#L50" >}}
{{< /tab >}}
{{< tab header="Ruby" >}}
{{< badge-code >}}
{{< gh-codeblock path="/examples/ruby/spec/browsers/chrome_spec.rb#L35-L37" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
{{< badge-code >}}
{{< gh-codeblock path="/examples/javascript/test/browser/chromeSpecificCaps.spec.js#L51-L55">}}
{{< /tab >}}
{{< tab header="Kotlin" >}}
{{< badge-code >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,19 @@ please use the `load-extension` argument instead, as mentioned in

{{< tabpane text=true langEqualsHeader=true >}}
{{< tab header="Java" >}}
{{< badge-code >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/browsers/ChromeTest.java#L44-L47" >}}
{{< /tab >}}
{{% tab header="Python" %}}
{{< badge-code >}}
{{< gh-codeblock path="/examples/python/tests/browsers/test_chrome.py#L41-L43">}}
{{% /tab %}}
{{< tab header="CSharp" >}}
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Browsers/ChromeTest.cs#L50" >}}
{{< /tab >}}
{{< tab header="Ruby" >}}
{{< badge-code >}}
{{< gh-codeblock path="/examples/ruby/spec/browsers/chrome_spec.rb#L35-L37" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
{{< badge-code >}}
{{< gh-codeblock path="/examples/javascript/test/browser/chromeSpecificCaps.spec.js#L51-L55">}}
{{< /tab >}}
{{< tab header="Kotlin" >}}
{{< badge-code >}}
Expand Down