码迷,mamicode.com
首页 > 其他好文 > 详细

Software Testing Lab2 (软件测试实验二) —— Selenium安装及入门

时间:2017-03-26 01:22:09      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:war   question   安装   sar   flow   ict   link   icon   through   

Download and install Firefox browser

If you are the user of WINDOWS, there is a link available for you.

Download and install selenium&firebug

There is the way that how I finish this step. Open Firefox, click the buttom like picture.

技术分享

Then, search selenium&firebug by this way. When you do this successfully, the icon like this will appear in the tool table.

技术分享

Record and export scripts by Selenium IDE

Record:

1. Open Selenium. Make sure it is working. (There is a red bottom on the right)

技术分享

2. Do some operate that you prefer to record on Firefox.

技术分享

3. Play the test case and debug.

技术分享

Export:

1. File->Export test case as->Java Junit4 WebDriver

技术分享

2. Create a new project and import this .java

3. Add .jar of selenium (Before that be sure that you have downloaded it)

技术分享

Please affirm the LIB of selenium jar is added too, otherwise it is no use. Also, junit is necessary.

技术分享

4. Debug

In this part, I met a bug when I run this project.

org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: WIN10

技术分享

 You can find the solution through stackoverflow. Add the code.

File pathToBinary = new File("C:\\user\\Programme\\FirefoxPortable\\App\\Firefox\\firefox.exe");//The way you store firefox.exe
FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
FirefoxProfile firefoxProfile = new FirefoxProfile();       
WebDriver driver = new FirefoxDriver(ffBinary,firefoxProfile);

Run result:

There is code, opening the url, loging in, asserting value of the special element and comparing them. 

package com.example.tests;

import java.util.regex.Pattern;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.support.ui.Select;

public class testSelenium {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    File pathToBinary = new File("d:\\program\\Firefox\\firefox.exe");
    FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
    FirefoxProfile firefoxProfile = new FirefoxProfile();       
    driver = new FirefoxDriver(ffBinary,firefoxProfile);
    baseUrl = "http://121.193.130.195:8080";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testSelenium() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.id("name")).clear();
    driver.findElement(By.id("name")).sendKeys("3014218140");
    driver.findElement(By.id("pwd")).clear();
    driver.findElement(By.id("pwd")).sendKeys("218140");
    driver.findElement(By.id("submit")).click();
    assertEquals("https://github.com/Danning1996", driver.findElement(By.xpath("//tbody[@id=‘table-main‘]/tr[3]/td[2]")).getText());
  }

  @After
  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;
    }
  }
}

The project will open the browser and do the same operation just as you did

技术分享

Coding the project and testing

 OMG,又遇到了一个7054的bug,简单点啊~QAQ

Software Testing Lab2 (软件测试实验二) —— Selenium安装及入门

标签:war   question   安装   sar   flow   ict   link   icon   through   

原文地址:http://www.cnblogs.com/Danning1996/p/6619007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!