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

 Unfortunatelly, it occurs another bug.

org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary(d:\program\Firefox\firefox.exe) on port 7055; process output follows: 

You can find the solution through stackoverflow. (hiahia, a good website)

That is said, the version has a mistake. hence, I check my version of fireFox. It is the newest one by updating itself in background. Well, when the version is 42.0, it does works!

Also, test the information in .csv. Read the information in .csv and put itinfo into different string. Open the file once. Read each line also test in the loop od while.

There is the code.

package com.example.tests;

import java.util.regex.Pattern;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
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();
  public static String stNum;
  public static String stPw;
  public static String gitAd;
  
  public String inString  = "";    
  public String delimeter = ",";
  public BufferedReader reader;
  
  public void read() {
        
        File readFile = new File("e:\\SoftwareTesting\\Lab2\\inputgit.csv");
        try{
            reader = new BufferedReader(new FileReader(readFile));
//            String inString  = "";        
//            String delimeter = ",";
//            String[] info;
//            int flag = 0;
//            
//            while((inString  = reader.readLine()) != null){
//                if(flag > 0){
//                    info = inString.split(delimeter);
//                    stNum = info[0];
//                    stPw = stNum.substring(4, 10);
//                    gitAd = info[2];
//                }
//                flag ++;
//            }
//            reader.close();
        }
        catch (FileNotFoundException ex) {
          System.out.println("No found!");
      } catch (IOException ex) {
          System.out.println("Reading error!");
      }
        
    }
  
  @Before
  public void setUp() throws Exception {
    //find the Firefox path binary 
    File pathToBinary = new File("d:\\program\\Firefox\\firefox.exe");
    FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
    FirefoxProfile firefoxProfile = new FirefoxProfile(); 
    //open the specified url
    
    driver = new FirefoxDriver(ffBinary,firefoxProfile);
    
    baseUrl = "http://121.193.130.195:8080";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    //initialize the info of a student
    //stNum = "3014218140";
    //stPw = "218140";
    //gitAd = "https://github.com/Danning1996";
  }

  @Test
  public void testSelenium() throws Exception {
    read();
    int flag = 0;
    String[] info;
    while((inString  = reader.readLine()) != null){
        if(flag > 0){
            info = inString.split(delimeter);
            stNum = info[0];
            stPw = stNum.substring(4, 10);
            gitAd = info[2];
            driver.get(baseUrl + "/");
            driver.findElement(By.id("name")).clear();
            driver.findElement(By.id("name")).sendKeys(stNum);
            driver.findElement(By.id("pwd")).clear();
            driver.findElement(By.id("pwd")).sendKeys(stPw);
            driver.findElement(By.id("submit")).click();
            
            assertEquals(gitAd, driver.findElement(By.xpath("//tbody[@id='table-main']/tr[3]/td[2]")).getText());
          
//            break;
        }
        flag ++;
    }
    reader.close();
//    driver.get(baseUrl + "/");
//    driver.findElement(By.id("name")).clear();
//    driver.findElement(By.id("name")).sendKeys(stNum);
//    driver.findElement(By.id("pwd")).clear();
//    driver.findElement(By.id("pwd")).sendKeys(stPw);
//    driver.findElement(By.id("submit")).click();
//    
//    assertEquals(gitAd, 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;
//    }
//  }
}

Let's see the result.

 

Although the result is right, it costs at least 8 minutes to test all the 117 lists. Maybe I should use multi-thread, I don't know that yet. Thus, I will learn it and update my code and blogs.

posted @ 2017-03-25 23:31  Danning1996  阅读(160)  评论(0编辑  收藏  举报