Software Testing Techniques LAB 02: Selenium

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 doesn’t use the Unicode to encoding.

So we can’t 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

时间: 2024-10-09 04:49:58

Software Testing Techniques LAB 02: Selenium的相关文章

Software Testing, Lab 1

Software Testing, Lab 1 一.实验要求: Install Junit(4.12), Hamcrest(1.3) with Eclipse Install Eclemma with Eclipse Write a java program for the triangle problem and test the program with Junit. a)       Description of triangle problem: Function triangle ta

Exploratory Software Testing

最近找到去年上半年看过一本关于测试方面书籍的总结笔记,一直放在我的个人U盘里,当时是用Xmind记录的,现在重新整理下分享给大家了! James A.Whittaker [美] 詹姆斯·惠特克(软件测试领域绝对的大师)著作<Exploratory Software Testing>,中文名<探索式软件测试>,记得当时被这本书深深吸引啦(我不知道有多少做测试的小伙伴看过这本书)!感觉是测试方面一本必不可少的书籍,瞬间感觉测试的魅力!废话不多说,直接来干货,希望可以给对探索式测试喜欢或

Software Testing Concepts

Software Testing Concepts

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 sele

SOFTWARE TESTING LAB 01

1.The brief description that you install junit, hamcrest and eclemma Junit和hamcrest直接从网上下载得到jar包,添加进项目的build PATH中:eclemma直接在eclipse中的Eclipse marketplace搜索安装得到. 2.The test result and coverage report (printscreen) of your tests on triangle problem.

Software Testing, Lab 1 Juint-and-Eclemma

Tasks: Install Junit(4.12), Hamcrest(1.3) with Eclipse Install Eclemma with Eclipse Write a java program for the triangle problem and test the program with Junit. a) Description of triangle problem: Function triangle takes three integers a,b,c which

Software Testing Report,Triangle Problem, Lab 1, May 5th . 2016

首先要在eclipse里安装junit, hamcrest 和 eclemma 插件,可以在http://junit.org/junit4/ 上找到插件的下载,国内网估计下不了需要FQ,我就用的myeclipse,里面直接自带.http://www.myeclipsecn.com/ 这是myeclipse的官网链接. 然后就是开始编程序和测试程序具体的Java在github上,链接:https://github.com/DongArvin/Triangle-Test1 最后的测试结果是绿了.但没

Software Testing lab02

1.安装firefox浏览器,最好为47.0.1版本或其他低版本 2.安装seleniumIDE,在附加组件中搜索selenium IDE 3.登陆http://121.193.130.195:8080/并开启录制,输入账号密码并登陆,随后使用ide运行测试案例 一开始我在使用最新版firefox时无法正确执行click行为,切换到47.0.1版本后就正常了. 最后点击文件->export test case as -> JAVA/JUnit4/WebDriver得到相应代码

Software Testing(软件测试 实验二)Lab2 Seleium

Selenium Experiment 1. Install the SeleiumIDE plugin (1)   Download firefox40.0.2 from https://ftp.mozilla.org/pub/firefox/releases/40.0.2/win32/zh-CN/ Add install the firefox browser following the instruction. (2)   Add Seleium plugin Search for "Se