• /
  • ログイン
  • 無料アカウント

スクリプト化ブラウザの例

スクリプト化ブラウザでは、Selenium WebDriverで動作するシンプルなJavaScriptのようなスクリプト言語を使用して、複雑なモニターのワークフローを構築できます。

使用可能なすべての関数に関する詳細なガイドについては、Syntheticsスクリプト化ブラウザのリファレンスをご覧ください。

ヒント

他のスクリプト化ブラウザの例を表示して共有するには、New RelicオンラインテクニカルコミュニティのSyntheticsスクリプトセクションをご覧ください。

URLの監視

この例では、モニターはhttp://telco.nrdemo-sandbox.com/を閲覧します。

//Visit http://telco.nrdemo-sandbox.com/
$browser.get("http://telco.nrdemo-sandbox.com/");

このスクリプトアクションは、ほぼすべてのスクリプト化ブラウザの基礎です。詳細については、URLにアクセスするをご覧ください。

リンクへの移動

以下の例では、モニターは次の項目を実行します。

  1. http://telco.nrdemo-sandbox.com/に移動します。
  2. リンクテキストにより概要ページを検索し、リンクをクリックします
  3. 一部の文字列Homeを検索してAcme Telco Homeリンクを検索し、リンクをクリックします
$browser.get("http://telco.nrdemo-sandbox.com/").then(function(){
//Find a link whose display text is `About` and click that link.
return $browser.findElement($driver.By.linkText("About")).click();
}).then(function(){
return $browser.findElement($driver.By.partialLinkText("Home")).click();
});

これらの手順はシーケンス関数によってソートされます。

要素の検出に関する詳細な指示および他のメソッドについては、要素を特定するをご覧ください。すべてのロケーターのリストについては、ロケーター:ページ要素の特定をご覧ください。

以下の例では、モニターは次の項目を実行します。

  1. http://telco.nrdemo-sandbox.com/static/companyBlog.jspに移動します。
  2. XPathにより検索ボックスに移動し、relicと入力します。
  3. XPathにより送信ボタンに移動し、ボタンをクリックして検索を送信します。
$browser.get("http://telco.nrdemo-sandbox.com/static/companyBlog.jsp").then(function(){
//Find the search field by specifying its id, then enter `relic`.
return $browser.findElement($driver.By.xpath("//h4[text()='Blog Search']/following-sibling::div/input")).sendKeys("relic");
}).then(function(){
//Click the search button.
return $browser.findElement($driver.By.xpath("//h4[text()='Blog Search']/following-sibling::div//button")).click();
});

テキストのフィールドへの送信については、テキストを入力するをご覧ください。

ページが読み込まれるまで待機

以下の例では、モニターは次の項目を実行します。

  1. http://telco.nrdemo-sandbox.com/browse/phonesに移動します。
  2. XPathによりAcme Standard電話の詳細ボタンを検索し、ボタンをクリックします。
  3. HTMLページのタイトルがAcme Commerce Companyと一致するまで10秒ほど待ちます。
  4. XPathによりカートに追加ボタンを検索し、ボタンをクリックします。
$browser.get("http://telco.nrdemo-sandbox.com/browse/phones").then(function(){
return $browser.findElement($driver.By.xpath("(//a[text()='Details'])[3]")).click();
});
//Call the wait function.
$browser.wait(function() {
//Tell the monitor to get the page title, then run a function on that title.
return $browser.getTitle().then(function(title) {
//Ensure that the title matches `Acme Commerce Company`.
return title === "Acme Commerce Company";
});
//If the condition isn't satisfied within 10000 milliseconds (10 seconds), proceed anyway.
}, 10000);
//Find the `Add to Cart` button via its XPath and click it.
$browser.findElement($driver.By.xpath("//input[@value='Add to Cart']")).click();

スクリプトを一時停止する待機条件の設定については、ページタイトルを待機するをご覧ください。

ページ要素の待機

以下の例では、モニターは次の項目を実行します。

  1. http://telco.nrdemo-sandbox.com/を読み込みます。
  2. HTML IDによりサポートドロップダウンを検索し、クリックします。
  3. FAQボタンが表示されるまで20秒ほど待ち、ボタンをクリックします。
//Navigate to the Acme Telco Homepage and clicks on the Support dropdown.
$browser.get("http://telco.nrdemo-sandbox.com/").then(function(){
return $browser.findElement($driver.By.id("supportDropDown")).click();
}).then(function(){
//Call the wait function to wait until the FAQ button appears.
return $browser.waitForAndFindElement($driver.By.id("supportFAQLink"), 2000).then(function(aboutPage){
return aboutPage.click();
})
});

詳細については、特定要素を待機するおよび条件: 一時停止および条件を待機するをご覧ください。

Webサイトへのログイン

以下の例では、モニターは次の項目を実行します。

  1. http://telco.nrdemo-sandbox.com/login.jspを読み込みます。
  2. 要素名によりユーザー名フィールドを検索し、当社の安全な認証情報機能を介して安全なユーザー名を送信します。
  3. 要素名によりパスワードフィールドを検索し、安全なパスワードを送信します。
  4. XPathによりログインボタンを検索し、クリックしてアカウント情報を送信します。
$browser.get("http://telco.nrdemo-sandbox.com/login.jsp").then(function(){
//Find the user name field by specifying its name, then submits a secured username.
return $browser.findElement($driver.By.name("username")).sendKeys($secure.SECURE_USERNAME);
}).then(function(){
//Find the password field by specifying its name, then submits a secured password.
return $browser.findElement($driver.By.name("password")).sendKeys($secure.SECURE_PASSWORD);
}).then(function(){
//Find and click the login button.
return $browser.findElement($driver.By.xpath("//input[@value='Login']")).click();
});

安全な認証情報を使用するには、安全な認証情報を保存するを参照してください。

ヒント

どのような認証情報を使用すべきですか?

複数のウェブサイトでパスワードを使い回すべきでないのと同様に、当社では、スクリプトに一意の新たな認証情報を作成することを推奨しています。個人の認証情報を使用したり、認証情報を再使用したりしないでください。

その他のヘルプ

追加のリソースは以下の通りです。

さらに支援が必要な場合は、これらのサポートと学習リソースを確認してください:

問題を作成するこのページを編集する
Copyright © 2020 New Relic Inc.