一. Firefox安装Selenium插件
在FireFox的菜单中的附加组件中搜索Selenium IDE
然后安装
二. 使用Selenium IDE录制脚本/导出脚本
点击图中标志打开Selenium IDE
红色按钮按下表示正在录制,这时候只用将界面切换到Firefox,网址中输入www.baidu.com,然后再搜索框中输入文字,点击搜索,所有的控件的访问都会被记录下来,然后切换回seleniumIDE就可以看到已经录制完毕
然后在图中红色选中的区域可以调整重新执行的速度,蓝色选中区中有start运行脚本,就可以重复执行之前坐的动作
然后可以点击 文件->save test case 或者文件->export test case(如果有一组,就可以save/export test suite)
三. 访问http://www.ncfxy.com使用学号登录系统,进入系统后可以看到该用户的邮箱
四. 编写Selenium Java WebDriver程序,测试info.csv表格中的学号和邮箱的对应关系是否正确
全部代码可见Github:https://github.com/clownice/SeleniumTest
主要代码如下
(使用Chrome.driver)
1 public void test() throws InterruptedException, IOException { 2 //Set Path of Chrome Driver 3 String chDriver = new File(new File(".").getCanonicalPath() + "\" +"driver/chromedriver.exe").getCanonicalPath(); 4 System.out.println(chDriver); 5 System.setProperty("webdriver.chrome.driver", chDriver); 6 //Set Path of Chrome.exe 7 System.setProperty("webdriver.chrome.bin", "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"); 8 driver = new ChromeDriver(); 9 10 //*******************************Open IE*************************************/ 11 /* String ieDriver = new File(new File(".").getCanonicalPath() + "\" +"driver/IEDriverServer.exe").getCanonicalPath(); 12 System.out.println(ieDriver); 13 System.setProperty("webdriver.ie.driver", ieDriver); 14 15 DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer(); 16 ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); 17 driver = new InternetExplorerDriver(ieCapabilities); 18 */ 19 //*******************************Open IE*************************************/ 20 21 //******************************Open Firefox************************************/ 22 //driver = new FirefoxDriver(); 23 //******************************Open Firefox************************************/ 24 25 26 //Set waiting time to avoid find null element before install it 27 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 28 //The Url we will 29 baseUrl = "http://www.ncfxy.com/index.html"; 30 String emailtemp = null; 31 String sidtemp = null; 32 String pwd = null; 33 //Use javacsv to process csv file, the reader r will read the file 34 CsvReader r = new CsvReader("G:\学习\大三\web开发\SelieumLab\info.csv",',',Charset.forName("GBK")); 35 //Read the head 36 r.readHeaders(); 37 38 //**********************************************************************************************// 39 //Each loop, it will get id from csv file and use substring to get the password, store the email// 40 //Then find element from web broser, then compare the email is equal to the one in the csv file // 41 //**********************************************************************************************// 42 43 while (r.readRecord()) { 44 45 sidtemp = r.get("id"); 46 if(sidtemp.equals("")) 47 break; 48 pwd = sidtemp.substring(sidtemp.length() - 6); 49 emailtemp = r.get("email"); 50 driver.get(baseUrl); 51 Thread.sleep(500); 52 driver.findElement(By.id("name")).sendKeys(sidtemp); 53 driver.findElement(By.id("pwd")).sendKeys(pwd); 54 driver.findElement(By.id("submit")).click(); 55 String email = driver.findElement(By.xpath(".//*[@id='table-main']/tr[1]/td[2]")).getText(); 56 System.out.println(sidtemp); 57 58 assertEquals(email,emailtemp); 59 } 60 r.close(); 61 62 } 63