第四次作业第二小题(SeleniumIDE的使用)

//(1)从ECLIPSE开始到浏览器

package com.lwk.test;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;


public class Test1 {

@Test
public void Test(){

//如果浏览器没有默认安装在C盘,需要制定其路径
System.setProperty("webdriver.firefox.bin", "E:\\Program Files\\Mozilla Firefox\\firefox.exe");
//System.setProperty("webdriver.ie.driver", "C:\\Program Files\\Internet Explorer\\IEDriverServer.exe");

//打开火狐浏览器
WebDriver driver = new FirefoxDriver();
//打开IE浏览器
/*WebDriver driver = new InternetExplorerDriver();*/
//如果做页面测试,建议用HttpUnitDriver,这种方式打开浏览器,而是在内存中运行,速度比较快
//WebDriver driver = new HtmlUnitDriver();

//打开要测试的页面
driver.get("http://www.baidu.com/");
System.out.println("打开链接——>");

//设置等待超出的时间(100秒)
WebDriverWait wait = new WebDriverWait(driver, 100);

//找到页面元素,此处是搜索输入框
WebElement txtSearchBox = driver.findElement(By.name("wd"));
//设置页面元素的值,即往输入框中输入值
txtSearchBox.sendKeys("selenium2");
//找到搜索按钮,并点击它
WebElement btn = driver.findElement(By.id("su"));
btn.click();

//关闭浏览器
//driver.close();
}
}

//=============================================================

(2)先录制,再转成JAVA代码

package com.lwk.test;

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 Test2 {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();

@Before
public void setUp() throws Exception {
System.setProperty("webdriver.firefox.bin", "E:\\Program Files\\Mozilla Firefox\\firefox.exe");
driver = new FirefoxDriver();
baseUrl = "https://www.baidu.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void testUntitled() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("kw")).click();
driver.findElement(By.id("kw")).click();
driver.findElement(By.id("kw")).click();
driver.findElement(By.id("kw")).clear();
driver.findElement(By.id("kw")).sendKeys("seleniumIDE");
driver.findElement(By.id("su")).click();
driver.findElement(By.id("kw")).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;
}
}
}

 

转载于:https://www.cnblogs.com/lwkblog/p/5512415.html

你可能感兴趣的:(第四次作业第二小题(SeleniumIDE的使用))