【PowerShell】Selenium-Module Start-SeRemote リモートブラウザの起動

目次

概要

 Webサービスでテスト用リモートブラウザを操作する際に、Start-SeRemotoメソッドを使用し、リモートブラウザを操作することができます。今回は、TestingBotというリモートブラウザのWebサービスを利用してSafariを利用してみたいと思います。

 PowerShellでSeleniumを利用するに当たって、ある程度簡易的にSeleniumを利用できる様に専用のモジュールがGitHubに用意されています。いきなりPowerShellでSeleniumを扱うより、Seleniumを-Moduleを利用した方が簡単にPowerShellでSleniumを利用することができます。環境構築するには、以下ページを参照してみてください。

Webサービスの登録(TestingBot)

 TestingBotは、28日間無料で利用することができますので、アカウント登録してトライアル利用します。詳しい登録の方法を知りたい方は、以下「TestingBotアカウント登録」をクリックしてフローを確認してみてください。

TestingBotアカウント登録
STEP
Webサイトに変遷

TestingBotのトライアルサイトに移り、必要事項を入力していきます。

ちなみに、TestingBotは日本語のサイトもありますが、日本語ページからサインアップ(アカウント登録)しようとしても画面が進みませんでした。

STEP
必要事項の入力

赤枠の箇所を入力します。英語サイトですので、必要事項は全て英語で入力しました。全て記入したら、チェックボックスにチェックを入れ、Sign Upボタンをクリックします。

STEP
アカウント登録確定

入力したメールアドレスにアドレス確認のためのメールが送信されます。Confirm your Accountをクリックしてアカウント登録を完了させます。

コマンド(Commond)

Start-SeRemote

代替コマンド(Alias)

無し

構文(Syntax)

Start-SeRemote [[-StartURL] <string>] [-RemoteAddress <string>] [-DesiredCapabilities <hashtable>] [-AsDefaultDriver] [-ImplicitWait <int>] [<CommonParameters>]

オプション

Name引数型解説Short Example
StartURLstring開きたいページのURLを指定Start-SeRemote -RemoteAddress $remoteDriverUrl -DesiredCapabilities $caps -StartURL "https://programan.org" -AsDefaultDriver
RemoteAddressstringリモートアクセスするアドレスを指定Start-SeRemote -RemoteAddress $remoteDriverUrl -DesiredCapabilities $caps -StartURL "https://programan.org" -AsDefaultDriver
DesiredCapabilitieshashtable利用するブラウザの種類を辞書型で指定Start-SeRemote -RemoteAddress $remoteDriverUrl -DesiredCapabilities $caps -StartURL "https://programan.org" -AsDefaultDriver
AsDefaultDriverカレントドライバとして設定Start-SeRemote -RemoteAddress $remoteDriverUrl -DesiredCapabilities $caps -StartURL "https://programan.org" -AsDefaultDriver
ImplicitWaitint待機時間の設定
CommonParameters共通パラメータ

※ShortExampleは、動作確認ができたコードを記載しています。

Example

事前準備(KeyとSecretの確認)

 TestingBotでブラウザを起動させるため、事前にTestingBotのマイページに移り、KeyとSecretをコピーしておきます。詳しい流れは、以下「KeyとSecretの確認(TestingBot)」をクリックして内容展開してみてください。

KeyとSecretの確認(TestingBot)
STEP
Accountページに移る

TestingBotのマイページホーム画面から右上のメニューより、Accountページに移ります。

STEP
KeyとSecretをコピー

アカウントページの上側に掲載されているKeyとSecretをコピーしておきます。

スクリプト実行

 以下にTestingBotをStart-SeRemoteで利用する例を紹介します。あらかじめ控えておいたKeyとSecretを1,2行目の変数に入力しておきます。

$key = "<Key>" #TestingBotで登録したKeyを入力
$secret = "<Secret>" #TestingBotで登録したSecretを入力
$RemoteDriverURL = [uri]"http://$key`:$secret@hub.testingbot.com/wd/hub"

$caps = @{
    platform     = 'HIGH-SIERRA'
    version      = '11'
    browserName  = 'safari'
}

Start-SeRemote -RemoteAddress $remoteDriverUrl -DesiredCapabilities $caps -StartURL "https://programan.org" -AsDefaultDriver
$Driver = $Global:SeDriver
$Element = Get-SeElement -Target $Driver -ClassName "newmark"
Invoke-SeClick -Element $Element[0]

Start-Sleep -Seconds 5
Stop-SeDriver $Driver

 初回接続に成功したらTestingBotから接続成功のメールが送られてきます。スクリプト実行の結果は、TestingBotのDashboardページから確認できます。動画でサファリのスクレイピングの録画を再生したり、スクレイピングで命令したクリック動作の命令文を時系列で追う事ができます。


 Webアプリを開発されている方は、各ブラウザのテスト結果を動画で確認できたり、命令文の結果を追う事ができるので大変便利ですね。

Module

Start-SeRemoteのSelenium-Module構文を以下に掲載します。

function Start-SeRemote {
    <#
        .example
        #you can a remote testing account with testing bot at https://testingbot.com/users/sign_up
        #Set $key and $secret and then ...
        #see also https://crossbrowsertesting.com/freetrial / https://help.crossbrowsertesting.com/selenium-testing/getting-started/c-sharp/
        #and https://www.browserstack.com/automate/c-sharp
        $RemoteDriverURL = [uri]"http://$key`:$secret@hub.testingbot.com/wd/hub"
        #See https://testingbot.com/support/getting-started/csharp.html for values for different browsers/platforms
        $caps = @{
          platform     = 'HIGH-SIERRA'
          version      = '11'
          browserName  = 'safari'
        }
        Start-SeRemote -RemoteAddress $remoteDriverUrl -DesiredCapabilties $caps
    #>
    [cmdletbinding(DefaultParameterSetName = 'default')]
    param(
        [string]$RemoteAddress,
        [hashtable]$DesiredCapabilities,
        [ValidateURIAttribute()]
        [Parameter(Position = 0)]
        [string]$StartURL,
        [switch]$AsDefaultDriver,
        [int]$ImplicitWait = 10
    )

    $desired = [OpenQA.Selenium.Remote.DesiredCapabilities]::new()
    if (-not $DesiredCapabilities.Name) {
        $desired.SetCapability('name', [datetime]::now.tostring("yyyyMMdd-hhmmss"))
    }
    foreach ($k in $DesiredCapabilities.keys) { $desired.SetCapability($k, $DesiredCapabilities[$k]) }
    $Driver = [OpenQA.Selenium.Remote.RemoteWebDriver]::new($RemoteAddress, $desired)

    if (-not $Driver) { Write-Warning "Web driver was not created"; return }

    $Driver.Manage().Timeouts().ImplicitWait = [TimeSpan]::FromSeconds($ImplicitWait)
    if ($StartURL) { $Driver.Navigate().GotoUrl($StartURL) }

    if ($AsDefaultDriver) {
        if ($Global:SeDriver) { $Global:SeDriver.Dispose() }
        $Global:SeDriver = $Driver
    }
    else { $Driver }
}
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

VBAを中心とした自動化、効率化の手法を紹介しています。現在は、SeleniumBasicのexamplesを紹介しています。その内、SeleniumBasic以外の手法も掲載したいと思っております。

目次