Selenium (3) —— Selenium IDE + Firefox錄制登錄腳本(101 Tutorial)


Selenium (3) —— Selenium IDE + Firefox錄制登錄腳本(101 Tutorial)


selenium IDE版本: 2.9.1

firefox版本: 39.0.3

參考來源:

Selenium官方下載

Selenium IDE

Understanding Selenium IDE vs Selenium RC

Selenium IDE Tutorial – Part 1

主要內容

Selenium IDE 是一個為進行Selenium測試的集成開發環境工具。Selenium測試可以用HTML table編寫,也可以用其他語言來編寫,比如C#,PHP,Perl,Python。IDE可以為我們錄制,編輯和調試測試。目前IDE以addon的形式只支持Firefox。

安裝

可以到以下地址安裝

http://selenium-ide.openqa.org/download.jsp

https://addons.mozilla.org/en-US/firefox/addon/2079

安裝完畢后需要重啟firefox,注意當前最新的IDE 2.9.1與Firefox 40+不太兼容,建議使用40以下的版本,我這里使用的是39.0.3

錄制

以登錄cnblogs為例

IDE啟動時,默認狀態下是正在錄制的

如果沒有處於錄制狀態,需要點擊右上角的紅色按鈕

  • 我們在瀏覽器上一次進行以下操作

    1. 輸入URL地址http://passport.cnblogs.com/user/signin,並訪問
    2. 輸入用戶名、密碼
    3. 點擊登錄

  • HTML table格式

    保存測試文件

    查看錄制文件

      	<?xml version="1.0" encoding="UTF-8"?>
      	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      	<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      	<head profile="http://selenium-ide.openqa.org/profiles/test-case">
      	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      	<link rel="selenium.base" href="http://passport.cnblogs.com/" />
      	<title>cnblogs</title>
      	</head>
      	<body>
      	<table cellpadding="1" cellspacing="1" border="1">
      	<thead>
      	<tr><td rowspan="1" colspan="3">cnblogs</td></tr>
      	</thead><tbody>
      	<tr>
      		<td>open</td>
      		<td>/user/signin?AspxAutoDetectCookieSupport=1</td>
      		<td></td>
      	</tr>
      	<tr>
      		<td>type</td>
      		<td>id=input1</td>
      		<td>weizhe_2008</td>
      	</tr>
      	<tr>
      		<td>type</td>
      		<td>id=input2</td>
      		<td>********</td>
      	</tr>
      	<tr>
      		<td>clickAndWait</td>
      		<td>id=signin</td>
      		<td></td>
      	</tr>
    
      	</tbody></table>
      	</body>
      	</html>
    
  • 導出其他格式(Java,Ruby)

    同樣在文件菜單下,我們可以選擇導出java或其他語言(File-> Export Test Case As...)

    • Java(Java / TestNG / WebDriver)

        package com.example.tests;
      
        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 Cnblogs {
          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://passport.cnblogs.com/";
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
          }
      
          @Test
          public void testCnblogs() throws Exception {
            driver.get(baseUrl + "/user/signin?AspxAutoDetectCookieSupport=1");
            driver.findElement(By.id("input1")).clear();
            driver.findElement(By.id("input1")).sendKeys("weizhe_2008");
            driver.findElement(By.id("input2")).clear();
            driver.findElement(By.id("input2")).sendKeys("********");
            driver.findElement(By.id("signin")).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;
            }
          }
        }
      
    • Ruby(Ruby / RSpec / WebDriver)

        	require "json"
            require "selenium-webdriver"
            require "rspec"
            include RSpec::Expectations
      
            describe "Cnblogs" do
      
              before(:each) do
                @driver = Selenium::WebDriver.for :firefox
                @base_url = "http://passport.cnblogs.com/"
                @accept_next_alert = true
                @driver.manage.timeouts.implicit_wait = 30
                @verification_errors = []
              end
              
              after(:each) do
                @driver.quit
                @verification_errors.should == []
              end
              
              it "test_cnblogs" do
                @driver.get(@base_url + "/user/signin?AspxAutoDetectCookieSupport=1")
                @driver.find_element(:id, "input1").clear
                @driver.find_element(:id, "input1").send_keys "weizhe_2008"
                @driver.find_element(:id, "input2").clear
                @driver.find_element(:id, "input2").send_keys "********"
                @driver.find_element(:id, "signin").click
              end
              
              def element_present?(how, what)
                ${receiver}.find_element(how, what)
                true
              rescue Selenium::WebDriver::Error::NoSuchElementError
                false
              end
              
              def alert_present?()
                ${receiver}.switch_to.alert
                true
              rescue Selenium::WebDriver::Error::NoAlertPresentError
                false
              end
              
              def verify(&blk)
                yield
              rescue ExpectationNotMetError => ex
                @verification_errors << ex
              end
              
              def close_alert_and_get_its_text(how, what)
                alert = ${receiver}.switch_to().alert()
                alert_text = alert.text
                if (@accept_next_alert) then
                  alert.accept()
                else
                  alert.dismiss()
                end
                alert_text
              ensure
                @accept_next_alert = true
              end
            end
      

回放

點擊Selenium IDE菜單欄上的綠色按鈕

執行結果

結束


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM