Lab2——Selenium
Posted mesty
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lab2——Selenium相关的知识,希望对你有一定的参考价值。
实验环境: Chrome 65.0.3325.181(64 位),ChromeDriver 2.37 ,poi-3.17,selenium-server-standalone-2.53.1,selenium-java-2.53.1
1、安装SeleniumIDE插件
谷歌插件搜索Selenium,添加插件如下
2、学会使用SeleniumIDE录制脚本和导出脚本
3、访问https://psych.liebes.top/st使用学号登录系统(账户名为学号,密码为学号后6位),进入系统后可以看到该同学的git地址。
导出代码
package com.example.tests; import java.util.regex.Pattern; 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 SeleniumTest { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception { driver = new FirefoxDriver(); baseUrl = "https://www.katalon.com/"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Test public void testSelenium() throws Exception { driver.get("https://psych.liebes.top/st"); driver.findElement(By.id("username")).click(); driver.findElement(By.id("username")).clear(); driver.findElement(By.id("username")).sendKeys("3015230112"); driver.findElement(By.id("password")).click(); driver.findElement(By.id("password")).clear(); driver.findElement(By.id("password")).sendKeys("230112"); driver.findElement(By.id("submitButton")).click(); driver.findElement(By.xpath("//p")).click(); } @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; } } }
4、编写Selenium Java WebDriver程序,测试input.xlsx表格中的学号和git地址的对应关系是否正确。
(1)使用POI读取excel文件,将学号和地址信息存入HashMap中
(2)利用SeleniumIDE导出的脚本,对每个HashMap的值进行测试,用assertEquals()方法测试excel中的地址和网页中的地址是否一致
测试代码:
package seleniumJavaWebDriver; import java.util.regex.Pattern; import java.io.File; import java.io.FileInputStream; import java.math.BigDecimal; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.TimeUnit; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; 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.chrome.*; import org.openqa.selenium.support.ui.Select; public class SeleniumWebDriver { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception { System.setProperty("webdriver.chrome.driver", "webDriver\\\\chromedriver.exe"); driver = new ChromeDriver(); baseUrl = "https://www.katalon.com/"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.get("https://psych.liebes.top/st"); } public Map<String,String> readExcel() throws Exception { //创建输入流 FileInputStream fis = new FileInputStream(new File("input.xlsx")); //由输入流得到工作簿 XSSFWorkbook workbook = new XSSFWorkbook(fis); //得到工作表 XSSFSheet sheet = workbook.getSheet("Sheet1"); Map<String,String> info = new HashMap<String,String>(); for(int i=sheet.getFirstRowNum();i<=sheet.getLastRowNum();i++){ XSSFRow row = sheet.getRow(i); String cellValue[] = new String[row.getLastCellNum()]; for(int j=row.getFirstCellNum();j<row.getLastCellNum();j++){ XSSFCell cell = row.getCell(j); switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: cellValue[j] = cell.getStringCellValue().trim(); break; case Cell.CELL_TYPE_NUMERIC: if(DateUtil.isCellDateFormatted(cell)) { cellValue[j] = cell.getDateCellValue().toString().trim(); }else { BigDecimal bd = new BigDecimal(cell.getNumericCellValue()); cellValue[j] = bd.toPlainString().trim(); } break; } } info.put(cellValue[0],cellValue[1]); } workbook.close(); fis.close(); return info; } @Test public void testSelenium() throws Exception { Map<String,String> info = readExcel(); Iterator<Entry<String, String>> iterator = info.entrySet().iterator(); while (iterator.hasNext()) { Entry<String, String> entry = iterator.next(); String username = entry.getKey(); String password = username.substring(4); String url = entry.getValue(); if(url==null) {continue;} driver.get("https://psych.liebes.top/st"); driver.findElement(By.id("username")).click(); driver.findElement(By.id("username")).clear(); driver.findElement(By.id("username")).sendKeys(username); driver.findElement(By.id("password")).click(); driver.findElement(By.id("password")).clear(); driver.findElement(By.id("password")).sendKeys(password); driver.findElement(By.id("submitButton")).click(); String geturl = driver.findElement(By.xpath("//p")).getText(); //System.out.println(username+" "+password+" "+url+" "+geturl); if(url.charAt(url.length()-1)==‘/‘) url = url.substring(0, url.length()-1); if(geturl.charAt(geturl.length()-1)==‘/‘) geturl = geturl.substring(0, geturl.length()-1); assertEquals(url, geturl); } } @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; } } }
以上是关于Lab2——Selenium的主要内容,如果未能解决你的问题,请参考以下文章
Software Testing Lab2 (软件测试实验二) —— Selenium安装及入门
Selenium Xpath元素无法定位 NoSuchElementException: Message: no such element: Unable to locate element(代码片段