Selenium上机实验

阅读: 评论:0

Selenium上机实验

Selenium上机实验

1、安装SeleniumIDE插件

在火狐浏览器菜单栏中选择添加插件,搜索SeleniumIDE,按步骤安装后重启浏览器即可。

 

安装成功后

 

2、学会使用SeleniumIDE录制脚本和导出脚本

打开SeleniumIDE,点击红色按钮开始录制,再次点击终止录制,在文件选项中可以选择保存格式。

 

 

3、访问(账户名为学号,密码为学号后6位),进入系统后可以看到该用户的邮箱。

 

4、编写Selenium Java WebDriver程序,测试info.csv表格中的学号和邮箱的对应关系是否正确。

5、将测试代码提交到github上。

import static org.junit.Assert.assertEquals;import java.nio.charset.Charset;
import java.util.ArrayList;import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;import com.csvreader.CsvReader;
public class TestSelenium {private ArrayList<String[]> csvList;WebDriver driver;@Beforepublic  void readCSV(){driver = new FirefoxDriver();try {       csvList = new ArrayList<String[]>(); //存csv中的信息String csvFilePath = "J:/软件测试/info.csv";CsvReader reader = new CsvReader(csvFilePath,',',Charset.forName("SJIS"));    //һ�����������Ϳ�����      adHeaders(); // 去标题adRecord()){ //读信息csvList.Values());   }               reader.close();   } catch (Exception ex) {   System.out.println(ex);   }   
}
@Test
public void testSelenium(){for(int row=0;row<csvList.size();row++) {("");//取出csv文件中的信息String name = (row)[0];String mail = (row)[1];//获取登录页面的元素WebElement username = driver.findElement(By.name("name"));WebElement pwd = driver.findElement(By.name("pwd"));WebElement sumit = driver.findElement(By.id("submit"));//填信息username.clear();username.sendKeys(name);pwd.clear();pwd.sendKeys(name.substring(4));sumit.click();//获取信息页面的邮箱值WebElement tableEle = driver.findElement(By.id("table-main"));String tableEleTextStr = Text();String mailEle = tableEleTextStr.substring(tableEleTextStr.indexOf("箱") + 2, tableEleTextStr.indexOf("学") - 1);String id = tableEleTextStr.substring( tableEleTextStr.indexOf("号") + 2);System.out.print(id);boolean match = name.equals(id) && mail.equals(mailEle);assertEquals(true, match);}
}@Afterpublic void close(){driver.close();}
}

  

/*** Created by shui on 2016/4/14.*/import Pattern;
import urrent.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 TestSelenu {private WebDriver driver;private String baseUrl;private boolean acceptNextAlert = true;private StringBuffer verificationErrors = new StringBuffer();@Beforepublic void setUp() throws Exception {driver = new FirefoxDriver();baseUrl = "/";driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);}@Testpublic void testTe() throws Exception {(baseUrl );driver.findElement(By.id("name")).clear();driver.findElement(By.id("name")).sendKeys("3013218101");driver.findElement(By.id("pwd")).clear();driver.findElement(By.id("pwd")).sendKeys("218101");driver.findElement(By.id("submit")).click();}@Afterpublic void tearDown() throws Exception {driver.quit();String verificationErrorString = String();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 = Text();if (acceptNextAlert) {alert.accept();} else {alert.dismiss();}return alertText;} finally {acceptNextAlert = true;}}
}

  

6测试结果

 

转载于:.html

本文发布于:2024-02-02 08:09:41,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170683257942491.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:Selenium
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23