Software Testing Techniques LAB 02: Selenium

本文介绍了使用Selenium进行自动化测试的过程,包括安装配置、数据处理、编码检查等环节,并通过具体示例展示了如何利用Firefox浏览器及Selenium IDE完成登录脚本的录制与导出。

1. Installing

1. Install firefox 38.5.1

2. Install SeleniumIDE 

    

 

After installing, I set the view of toolbox, then we can see this

 

 

 

3. Install Selenium Client & WebDrive

  

      

4. Install Selenium Standalone Server

5. Installed Test

After downloading we have these files

Then we test Selenium IDE on firefox firstly,

I recorded the script about signing in the Software Testing Techniques Website

 

Then I export the script,

 

 

 

  Then I test the web driver,

 

  First, I wrote the code about searching by Baidu.com

 

 

This is the result,

 

The installing is finished now!

 

2. Data processing

There is a big problem that the inputgit.csv file doesnt use the Unicode to encoding.

So we cant process it by JAVA.String function, and normally we open it with gibberish.

 

I have to change the encoding to UTF-8 WITH BOM

 

3. Coding and checking

1. Coding

The whole project looks like this

 

1.1 The csv files reader

readFile.java

Cut the last 6 num from student num as password

 

pwd[idx] = num[idx].substring(4);

 

This is the whole code

package Test1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class readFile {
    
    public String[] num = new String[120];
    public String[] name = new String[120];
    public String[] add = new String[120];
    public String[] pwd = new String[120];
    
    public void read (){          
          int idx = 0;
          File csv = new File("D:\\java\\workplace\\seleniumTest\\bin\\Test1\\inputgit.csv");
          BufferedReader br = null;
          try
          {
              br = new BufferedReader(new FileReader(csv));
          } catch (FileNotFoundException e)
          {
              e.printStackTrace();
          }
          String line = "";
          String[] everyLine = new String[3];
          try {
              line = br.readLine();
              while ((line = br.readLine()) != null)
              {
                  everyLine =line.split(",");
                  num[idx] = everyLine[0];
                  name[idx] = everyLine[1];
                  add[idx] = everyLine[2];
                  pwd[idx] = num[idx].substring(4);
                  
                  idx++;
              }
          } catch (IOException e)
          {
              e.printStackTrace();
          }    
    }
    
}

 

 

 

1.2 The NumTest.java

 

This file just come from the NumTest.java, but I add some functions and variables to store and return a Git address from test student number.

 

The most important part is set the web driver location

 

System.setProperty("webdriver.firefox.bin", "D:\\oldfirefox\\firefox.exe");
System.setProperty("webdriver.firefox.marionette", "D:\\oldfirefox\\geckodriver.exe");

 

 

 

Change function to receive the number and password from csv reader.

 

public void testNum(String num, String pwd)

 

 

 

This is the whole code

 

package Test1;

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.ArrayList;
import java.util.List;
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.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

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

  @Before
  public void setUp() throws Exception {
    System.setProperty("webdriver.firefox.bin", "D:\\oldfirefox\\firefox.exe");
    System.setProperty("webdriver.firefox.marionette", "D:\\oldfirefox\\geckodriver.exe");
    driver = new FirefoxDriver();
    baseUrl = "Invisible";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testNum(String num, String pwd) throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.id("reset")).click();
    driver.findElement(By.id("name")).clear();
    driver.findElement(By.id("name")).sendKeys(num);
    driver.findElement(By.id("pwd")).clear();
    driver.findElement(By.id("pwd")).sendKeys(pwd);
    driver.findElement(By.id("submit")).click();
    add = driver.findElement(By.xpath("//tbody[@id='table-main']/tr[3]/td[2]")).getText();
    System.out.println(add);
  }

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

 

 

 

1.3 The mainCheck.java

 

This file contains the main function, and is control the whole program

 

package Test1;

public class mainCheck {
    
    public static void main(String args[]) { 
        readFile readFile = new readFile();
        readFile.read();
        
        NumTest ntest = new NumTest();
        try {            
            for (int idx = 0; idx < readFile.num.length;idx++ )
            {
                System.out.println(idx);
                ntest.setUp();
                ntest.testNum(readFile.num[idx], readFile.pwd[idx]);
                if (!ntest.add.equals(readFile.add[idx]) )
                    System.out.println(readFile.num[idx]+"wrong!");
                else 
                    System.out.println(readFile.num[idx]+"right!");
                ntest.tearDown();
            }    
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}

 

 

 

4. Result

I print all student number and if the github address is right then print right!

If wrong then print wrong!

 

But because this program is too slow to open firefox every time, so I didnt run it for all student numbers

The more detail please come to my blog:

http://www.cnblogs.com/nocis/p/6618790.html

The code has been uploaded to my Github:

https://github.com/nocis/Software-Test/tree/master/seleniumTest

转载于:https://www.cnblogs.com/nocis/p/6618790.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值