seleniumIDE安裝和錄製導出測試腳本的步驟

一、  先安裝火狐瀏覽器Firefoxphp

二、  在火狐瀏覽器Firefox輸入欄,java

輸入https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/,按回車鍵,顯示以下圖:web

三、  點擊按鈕【Add to Firefox】,彈出下圖瀏覽器

四、  點擊按鈕【安裝】,安裝結束後,重啓瀏覽器,就能夠在開發者菜單看到Selenium IDE選項,以下圖:ide

五、 打開Selenium IDE,界面以下圖:   測試

六、打開火狐瀏覽器,輸入測試訪問地址http://192.168.199.110/chadmin/backend/web/index.php,登陸操做ui

七、點擊文件,選擇Export Test Case As,選擇Java/TestNG/WebDriver,導出腳本。.net

八、 導出腳本以下:firefox

package com.example.tests;ci

 

import java.util.regex.Pattern;

import java.util.concurrent.TimeUnit;

import org.testng.annotations.*;

import static org.testng.Assert.*;

import org.openqa.selenium.*;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.Select;

 

public class Login {

  private WebDriver driver;

  private String baseUrl;

  private boolean acceptNextAlert = true;

  private StringBuffer verificationErrors = new StringBuffer();

 

  @BeforeClass(alwaysRun = true)

  public void setUp() throws Exception {

    driver = new FirefoxDriver();

    baseUrl = "http://192.168.199.110/chadmin/backend/web/index.php";

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

  }

 

  @Test

  public void testLogin() throws Exception {

    driver.get(baseUrl + "/chadmin/backend/web/index.php");

    driver.findElement(By.id("username")).clear();

    driver.findElement(By.id("username")).sendKeys("admin");

    driver.findElement(By.id("password")).clear();

    driver.findElement(By.id("password")).sendKeys("123456");

    driver.findElement(By.id("login_btn")).click();

  }

 

  @AfterClass(alwaysRun = true)

  public void tearDown() throws Exception {

    driver.quit();

    String verificationErrorString = verificationErrors.toString();

    if (!"".equals(verificationErrorString)) {

      fail(verificationErrorString);

    }

  }

 

  private boolean isElementPresent(By by) {

    try {

      driver.findElement(by);

      return true;

    } catch (NoSuchElementException e) {

      return false;

    }

  }

 

  private boolean isAlertPresent() {

    try {

      driver.switchTo().alert();

      return true;

    } catch (NoAlertPresentException e) {

      return false;

    }

  }

 

  private String closeAlertAndGetItsText() {

    try {

      Alert alert = driver.switchTo().alert();

      String alertText = alert.getText();

      if (acceptNextAlert) {

        alert.accept();

      } else {

        alert.dismiss();

      }

      return alertText;

    } finally {

      acceptNextAlert = true;

    }

  }

}

相關文章
相關標籤/搜索