码迷,mamicode.com
首页 > 其他好文 > 详细

Selenium上机实验

时间:2018-04-14 23:57:51      阅读:508      评论:0      收藏:0      [点我收藏+]

标签:support   isa   点击   ext   附件   log   cond   img   second   

1、安装SeleniumIDE插件

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

3、访问https://psych.liebes.top/st使用学号登录系统(账户名为学号,密码为学号后6位),进入系统后可以看到该同学的git地址。

4、编写Selenium Java WebDriver程序,测试input.xlsx表格中的学号和git地址的对应关系是否正确。

5、将测试代码提交到github

 

1、下载Firefox,安装并在工具栏中点击工具找到添加附件,点击,在右下角点击添加更多附件,在右上角搜索SeleniumIDE

2、点击菜单栏中的工具,点击SeleniumIDE,如下

技术分享图片

浏览网页并导出代码:

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 1 {
  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.duba.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void test1() throws Exception {
    driver.get(baseUrl + "/?un_454973_1");
    driver.findElement(By.xpath("(//a[contains(text(),‘鐧惧害‘)])[2]")).click();
    driver.findElement(By.id("kw")).click();
    driver.findElement(By.id("kw")).clear();
    driver.findElement(By.id("kw")).sendKeys("浠g爜");
    driver.findElement(By.id("su")).click();
    driver.findElement(By.linkText("缂栫▼浠g爜澶у叏")).click();
    driver.findElement(By.linkText("浠g爜澶у叏_鐧惧害鐧剧")).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;
    }
  }
}

 

3.

技术分享图片

 

4.测试代码及结果

package st2;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;

import com.csvreader.CsvReader;


import java.util.List;

@RunWith(Parameterized.class)
public class test {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();
  private String id,pwd,github;
  
  public test(String id,String github)
  {
	  this.id = id;
	  
	  this.github = github;
	  this.pwd = id.substring(4);
	  System.out.println(this.id+" "+this.github+" "+this.pwd);
	  
  }
  
  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "https://psych.liebes.top";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get(baseUrl+"/st");
  }
  
  @Parameters
  public static Collection<Object[]> getData() throws Exception
  {
	  Object[][] obj = new Object[117][];
	  CsvReader r = new CsvReader("C:\\Users\\imac\\Desktop\\2018\\软件测试\\input.csv",‘,‘,
			  Charset.forName("GBK"));
	  //System.out.println(r);
	  int count = 0;
	  r.readHeaders();
	  while(r.readRecord())
	  {
		  obj[count] = new Object[]{r.get("id"), r.get("git") };
		  count++;
		  //System.out.println(r.get("git"));
	  }
	  System.out.println(count);
	  return Arrays.asList(obj);
  }
  

  @Test
  public void test10() throws Exception {
   
    driver.findElement(By.id("username")).clear();
    driver.findElement(By.id("username")).sendKeys(this.id);
    driver.findElement(By.id("password")).clear();
    driver.findElement(By.id("password")).sendKeys(this.pwd);
    driver.findElement(By.id("submitButton")).click();
    //driver.findElement(By.cssSelector("p.login-box-msg")).click();
    
    assertEquals(this.github, driver.findElement(By.cssSelector("p.login-box-msg")).getText());
  }

  @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;
    }
  }
}

 

技术分享图片

 

Selenium上机实验

标签:support   isa   点击   ext   附件   log   cond   img   second   

原文地址:https://www.cnblogs.com/c-czl123/p/8835698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!