Chrome デベロッパーツール

Selenium 4 alphaバージョンには、“DevTools” インターフェースを介した Chrome Dev Protocolのネイティブサポートが待望されています。 これにより、アプリケーションキャッシュ、フェッチ、ネットワーク、パフォーマンス、 プロファイラー、リソースタイミング、セキュリティ、 ターゲットCDPドメインなどのChrome開発プロパティを取得できます。

Chrome デベロッパーツールは、Google Chromeブラウザに直接組み込まれた一連のWeb開発ツールです。 DevToolsは、ページをすぐに編集して問題をすばやく診断するのに役立ち、 最終的にはより優れたWebサイトをより速く構築するのに役立ちます。

ジオロケーションをエミュレート

一部のアプリケーションには、場所によって特徴や機能性が異なります。 このようなアプリケーションの自動化は、ブラウザでSeleniumを使用して 地理的位置をエミュレートすることが難しいため、困難です。 しかし、デベロッパーツールの助けを借りて、それらを簡単にエミュレートできます。 以下のコードスニペットはそのことを示しています。


import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;

public void geoLocationTest(){
  ChromeDriver driver = new ChromeDriver();
  Map coordinates = new HashMap()
  {{
      put("latitude", 50.2334);
      put("longitude", 0.2334);
      put("accuracy", 1);
  }};    
  driver.executeCdpCommand("Emulation.setGeolocationOverride", coordinates);
  driver.get("<your site url>");
}  
  

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

def geoLocationTest():
driver = webdriver.Chrome()
Map_coordinates = dict({
"latitude": 41.8781,
"longitude": -87.6298,
"accuracy": 100
})
driver.execute_cdp_cmd("Emulation.setGeolocationOverride", Map_coordinates)
driver.get("<your site url>")
  

using System.Threading.Tasks;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.DevTools;
// Replace the version to match the Chrome version
using OpenQA.Selenium.DevTools.V87.Emulation;

namespace dotnet_test {
  class Program {
    public static void Main(string[] args) {
      GeoLocation().GetAwaiter().GetResult();
    }
        
    public static async Task GeoLocation() {
      ChromeDriver driver = new ChromeDriver();
      DevToolsSession devToolsSession = driver.CreateDevToolsSession();
      var geoLocationOverrideCommandSettings = new SetGeolocationOverrideCommandSettings();

      geoLocationOverrideCommandSettings.Latitude = 51.507351;
      geoLocationOverrideCommandSettings.Longitude = -0.127758;
      geoLocationOverrideCommandSettings.Accuracy = 1;

      await devToolsSession
        .GetVersionSpecificDomains<OpenQA.Selenium.DevTools.V87.DevToolsSessionDomains>()
        .Emulation
        .SetGeolocationOverride(geoLocationOverrideCommandSettings);

        driver.Url = "<your site url>";
        }
    }
}
  

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome

begin
  # Latitude and longitude of Tokyo, Japan
  coordinates = { latitude: 35.689487,
                  longitude: 139.691706,
                  accuracy: 100 }
  driver.execute_cdp('Emulation.setGeolocationOverride', coordinates)
  driver.get 'https://www.google.com/search?q=selenium'
ensure
  driver.quit
end
  

// Please raise a PR to add code sample  
  

import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.devtools.DevTools

fun main() {
    val driver =  ChromeDriver()
    val coordinates : HashMap<String, Any> = HashMap<String, Any> ()
    coordinates.put("latitude", 50.2334)
    coordinates.put("longitude", 0.2334)
    coordinates.put("accuracy", 1)
    driver.executeCdpCommand("Emulation.setGeolocationOverride", coordinates)
    driver.get("https://www.google.com")
} 
  

Register Basic Auth:

Some applications require to keep some pages behind an auth and most of the time to keep things simple, a developer uses Basic Auth. With Selenium and devtools integration, you can automate the input of basic auth credentials whenever they arise.


Predicate<URI> uriPredicate = uri -> uri.getHost().contains("your-domain.com");

((HasAuthentication) driver).register(uriPredicate, UsernameAndPassword.of("admin", "password"));
driver.get("https://your-domain.com/login");
  

# Please raise a PR to add code sample
  

# Please raise a PR to add code sample
  

# Please raise a PR to add code sample
  

const pageCdpConnection = await driver.createCDPConnection('page')

await driver.register('username', 'password', pageCdpConnection)
await driver.get(server.url())
  

# Please raise a PR to add code sample
  

Listen to DOM events on a web page

Using Selenium’s integration with CDP, one can listen to the DOM events and register callbacks to process the DOM event.


# Please raise a PR to add code sample
  

# Please raise a PR to add code sample
  

# Please raise a PR to add code sample
  

# Please raise a PR to add code sample
  

const cdpConnection = await driver.createCDPConnection('page')
await driver.logMutationEvents(cdpConnection, function(event) {
  assert.equal(event['attribute_name'], 'style')
  assert.equal(event['current_value'], '')
  assert.equal(event['old_value'], 'display:none;')
})

await driver.get(test.Pages.dynamicPage)

let element = driver.findElement({id: 'reveal'})
await element.click()
let revealed = driver.findElement({id: 'revealed'});
await driver.wait(until.elementIsVisible(revealed), 5000);
  

# Please raise a PR to add code sample
  

Listen to JS Exceptions on a web page

Using Selenium’s integration with CDP, one can listen to the JS Exceptions and register callbacks to process the exception details.


# Please raise a PR to add code sample
  

# Please raise a PR to add code sample
  

# Please raise a PR to add code sample
  

# Please raise a PR to add code sample
  

const cdpConnection = await driver.createCDPConnection('page')
await driver.onLogException(cdpConnection, function(event) {
  assert.equal(event['exceptionDetails']['stackTrace']['callFrames'][0]['functionName'], 'onmouseover')
})
await driver.get(test.Pages.javascriptPage)
let element = driver.findElement({id: 'throwing-mouseover'})
await element.click()
  

# Please raise a PR to add code sample
  

Listen to console.log events on a web page

Using Selenium’s integration with CDP, one can listen to the console.log events and register callbacks to process the event.


# Please raise a PR to add code sample
  

# Please raise a PR to add code sample
  

# Please raise a PR to add code sample
  

# Please raise a PR to add code sample
  

const cdpConnection = await driver.createCDPConnection('page')
await driver.onLogEvent(cdpConnection, function(event) {
  assert.equal(event['args'][0]['value'], 'here')
})
await driver.executeScript('console.log("here")')
  

# Please raise a PR to add code sample