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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Software Testing Lab2 (软件测试实验二) —— Selenium安装及入门相关的知识,希望对你有一定的参考价值。

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安装及入门的主要内容,如果未能解决你的问题,请参考以下文章

Software Testing Lab2

Software Testing (软件测试作业三) HW3 prin

Software Testing soot —— 软件测试soot安装及使用

Software Testing Lab 1

software Testing Lab1

Software Testing hw2