Selenium-webdriver

提供:Dev Guides
移動先:案内検索

セレン-Webdriver

WebDriverは、Webアプリケーションのテストを自動化するためのツールです。 一般にSelenium 2.0として知られています。 WebDriverは別の基盤フレームワークを使用しますが、Selenium RCはブラウザーに埋め込まれたJavaScript Selenium-Coreを使用しますが、これにはいくつかの制限があります。 WebDriverは、サーバーに依存するSelenium RCとは異なり、仲介なしでブラウザーと直接対話します。 次のコンテキストで使用されます-

  • Selenium RC(Selenium 1.0)で十分にサポートされていないブラウザ向けの改善された機能を含むマルチブラウザテスト。
  • 複数のフレーム、複数のブラウザーウィンドウ、ポップアップ、およびアラートの処理。
  • 複雑なページナビゲーション。
  • ドラッグアンドドロップなどの高度なユーザーナビゲーション。
  • AJAXベースのUI要素。

建築

WebDriverは、以下に示す単純なアーキテクチャ図で最もよく説明されます。

Selenium IDE 92

Selenium RC Vs WebDriver

Selenium RC Selenium WebDriver
The architecture of Selenium RC is complicated, as the server needs to be up and running before starting a test. WebDriver’s architecture is simpler than Selenium RC, as it controls the browser from the OS level.
Selenium server acts as a middleman between the browser and Selenese commands. WebDriver interacts directly with the browser and uses the browser’s engine to control it.
Selenium RC script execution is slower, since it uses a Javascript to interact with RC. WebDriver is faster, as it interacts directly with the browser.
Selenium RC cannot support headless execution as it needs a real browser to work with. WebDriver can support the headless execution.
It’s a simple and small API. Complex and a bit large API as compared to RC.
Less object-oriented API. Purely object oriented API.
Cannot test mobile Applications. Can test iPhone/Android applications.

WebDriverを使用したスクリプティング

WebDriverの使用方法を理解しましょう。 デモンストレーションには、https://www.calculator.net/を使用します。 「Math Calculator」の下にある「Percent Calculator」を実行します。 必要なWebDriver JARをすでにダウンロードしています。 詳細については、「環境設定」の章を参照してください。

  • ステップ1 *-抽出されたEclipseフォルダーから「Eclipse」を起動します。

Selenium IDE 75

  • ステップ2 *-[参照]ボタンをクリックしてワークスペースを選択します。

Selenium IDE 76

  • ステップ3 *-「ファイル」メニューから「新しいプロジェクト」を作成します。

Selenium IDE 53

  • ステップ4 *-プロジェクト名を入力し、「次へ」をクリックします。

Selenium IDE 77

  • ステップ5 *-[ライブラリ]タブに移動し、ダウンロードしたすべてのJARを選択します。 Selenium WebDriver LibraryフォルダーのすべてのJARへの参照と、selenium-java-2.42.2.jarおよびselenium-java-2.42.2-srcs.jarへの参照を追加します。

Selenium IDE 78

  • ステップ6 *-パッケージは次のように作成されます。

Selenium IDE 79

  • ステップ7 *-パッケージを右クリックし、「新規」>>「クラス」を選択して「クラス」を作成します。

Selenium IDE 82

  • ステップ8 *-クラスに名前を付けて、メイン関数にします。

Selenium IDE 80

  • ステップ9 *-クラスの概要を以下に示します。

Selenium IDE 81

  • ステップ10 *-さあ、コーディングの時間です。 次のスクリプトは、手順を明確に説明するコメントが埋め込まれているため、理解しやすくなっています。 オブジェクトのプロパティをキャプチャする方法を理解するには、「ロケーター」の章をご覧ください。
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class webdriverdemo {
   public static void main(String[] args) {

      WebDriver driver = new FirefoxDriver();
     //Puts an Implicit wait, Will wait for 10 seconds before throwing exception
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

     //Launch website
      driver.navigate().to("http://www.calculator.net/");

     //Maximize the browser
      driver.manage().window().maximize();

     //Click on Math Calculators
      driver.findElement(By.xpath(".//*[@id = 'menu']/div[3]/a")).click();

     //Click on Percent Calculators
      driver.findElement(By.xpath(".//*[@id = 'menu']/div[4]/div[3]/a")).click();

     //Enter value 10 in the first number of the percent Calculator
      driver.findElement(By.id("cpar1")).sendKeys("10");

     //Enter value 50 in the second number of the percent Calculator
      driver.findElement(By.id("cpar2")).sendKeys("50");

     //Click Calculate Button
      driver.findElement(By.xpath(".//*[@id = 'content']/table/tbody/tr[2]/td/input[2]")).click();


     //Get the Result Text based on its xpath
      String result =
         driver.findElement(By.xpath(".//*[@id = 'content']/p[2]/font/b")).getText();


     //Print a Log In message to the screen
      System.out.println(" The Result is " + result);

     //Close the Browser.
      driver.close();
   }
}
  • ステップ11 *-上記のスクリプトの出力はコンソールに出力されます。

Selenium IDE 83

最も使用されるコマンド

以下の表は、WebDriverで最も頻繁に使用されるコマンドの一部とその構文を示しています。

Sr.No. Command & Description
1

driver.get("URL")

アプリケーションに移動します。

2

element.sendKeys("inputtext")

入力ボックスにテキストを入力します。

3

element.clear()

入力ボックスから内容を消去します。

4

select.deselectAll()

ページの最初のSELECTからすべてのオプションを選択解除します。

5

select.selectByVisibleText("some text")

ユーザーが指定した入力でOPTIONを選択します。

6

driver.switchTo().window("windowName")

あるウィンドウから別のウィンドウにフォーカスを移動します。

7

driver.switchTo().frame("frameName")

フレームからフレームへスイングします。

8

driver.switchTo().alert()

アラートの処理に役立ちます。

9

driver.navigate().to("URL")

URLに移動します。

10

driver.navigate().forward()

前方に移動します。

11

driver.navigate().back()

ナビゲートします。

12

driver.close()

ドライバーに関連付けられている現在のブラウザーを閉じます。

13

driver.quit()

ドライバーを終了し、そのドライバーに関連付けられているすべてのウィンドウを閉じます。

14

driver.refresh()

現在のページを更新します。