Selenium上机实验

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

 

 

转载于:https://www.cnblogs.com/c-czl123/p/8835698.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值