体验Selenium
实验要求:
1、安装SeleniumIDE插件
2、学会使用SeleniumIDE录制脚本和导出脚本
3、访问http://121.193.130.195:8080/使用学号登录系统(账户名为学号,密码为学号后6位),进入系统后可以看到该同学的git地址。
4、编写Selenium Java WebDriver程序,测试inputgit.csv表格中的学号和git地址的对应关系是否正确。
实验步骤:
1、安装SeleniumIDE插件:
下载SeleniumIDE插件, 在火狐浏览器中添加插件
可以参考一下以下地址:
http://jingyan.baidu.com/article/48b558e32555bc7f38c09ae3.html
2、学会使用SeleniumIDE录制脚本和导出脚本
打开Selenium插件,得到如下界面
然后在浏览器里面操作一波,得到如下结果:
点击上面绿色三角形的按钮,可以发现Selenium重新执行了一边之前的操作,非常Cool。
3、访问http://121.193.130.195:8080/使用学号登录系统(账户名为学号,密码为学号后6位),进入系统后可以看到该同学的git地址。
比较简单,不用多说了。
4、编写Selenium Java WebDriver程序,测试inputgit.csv表格中的学号和git地址的对应关系是否正确。
首先将Excel转成TXT,保存在D盘下的a/input.txt内,方便处理,然后提取文本中的信息。再通过FirefoxWebDriver 对浏览器进行操作,提取关键
元素然后检测是否相等,最后输出是否相等,若相等会输出 名字is right, 否则输出 名字is wrong
贴出代码:
package TEST;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.*;
import java.util.Scanner;
import org.openqa.selenium.chrome.*;
public class sele {
public static void main(String[] args) {
// TODO Auto-generated method stub
String [][] str = new String[117][3];
int counter = 0;
System.out.println("" + str.length);
try {
Scanner in = new Scanner(new File("d:/a/input.txt"));
while (in.hasNextLine()){
String st = in.nextLine();
str[counter] = st.split("\\s+");
counter++;
}
}
catch (FileNotFoundException e){
e.printStackTrace();
}
System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
for (int i = 0; i < str.length; i++)
{
driver.get("http://121.193.130.195:8080/");
WebElement element = driver.findElement(By.id("name"));
element.sendKeys(str[i][0]);
element = driver.findElement(By.id("pwd"));
element.sendKeys(str[i][0].substring(4, 10));
element.submit();
element = driver.findElement(By.id("resultString"));
String html = element.getAttribute("innerHTML");
String [] result = html.split(",");
String addr = result[2].trim();
if (addr.equals(str[i][2]))
{
System.out.println( str[i][1] + "is right");
}
else
{
System.out.println( str[i][1] + "is wrong");
}
}
}
}
可以得到如下结果: