package web.selenium2;
import junit.framework.TestCase;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
/**
* 自动化测试:页面操作的基本功能
* @author Administrator
*/
public class FirstCase extends TestCase {
//声明一个chrome driver对象
WebDriver driver = null;
//每个测试方法执行前执行
public void setUp() throws Exception{
System.out.println("init......");
//将当前浏览器的测试驱动注入到webdriver.chrome.driver中
System.setProperty("webdriver.chrome.driver", "C:/Users/Administrator/Desktop/python/chromedriver.exe");
driver = new ChromeDriver();
}
//每个测试方法结束后执行
public void tearDown() throws Exception{
System.out.println("destory......");
//关闭浏览器
driver.quit();
}
//测试百度登录
public void testBaiduSearch() {
//指定窗口的位置
//Point p = new Point(300, 300);
//driver.manage().window().setPosition(p);
//打开baidu
driver.get("http://www.baidu.com");
//将后台脚本注入到前台执行
String js ="alert(\"hello,this is a alert!\")";
//firefox没有Object[]参数
//((JavascriptExecutor) driver).executeScript(js,new Object[]{});
//等待页面弹出的alert窗口关闭,否则后续代码执行窗口事件不匹配,测试不通过
/*try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}*/
//定位搜索框
WebElement searchInput = driver.findElement(By.id("kw"));
//搜索框输入关键字
//CharSequence c= "12";//不能通过new创建
CharSequence cs[] = { "python"};
searchInput.sendKeys(cs);
//定位搜索按钮
WebElement searchButton = driver.findElement(By.id("su"));
//点击搜索按钮
searchButton.click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//关闭浏览器
driver.quit();
}
//测试ITeye登录
public void testITeyeLogin() {
try{
driver.manage().window().maximize();//窗口最大化
//打开iteye
driver.get("http://zengshaotao.iteye.com/login");
System.out.println(driver.getTitle());
//定位搜索框
WebElement usernameInput = driver.findElement(By.id("user_name"));
WebElement pwdInput = driver.findElement(By.id("password"));
CharSequence username[] = { "zengshaotao"};
CharSequence password[] = { "******"};
//在页面上输入用户名和密码
usernameInput.sendKeys(username);
pwdInput.sendKeys(password);
//定位登录按钮
WebElement searchButton = driver.findElement(By.id("button"));
//searchButton.getText();
//点击登录按钮
searchButton.click();
//此处是刚登录后页面内容的检查
//自动化操作代码需要与具体的页面场景相对应,也即这里是登录成功后的页面场景,不能定位登录之前的页面内容
//否则会因为定位不到出现错误提示
WebElement loginResult = driver.findElement(By.id("user_visits"));
//从登录结果页面定位user_visits对象
String cssValue = loginResult.getAttribute("class");
if("clearfix".equals(cssValue)){
System.out.println("登录成功。。。。。");
}else{
System.out.println("登录失败。。。。。");
}
//如果元素显示了返回true,否则返回false
if(loginResult.isDisplayed()){
System.out.println("元素已经显示。。。。。。");
}
//通过class属性在搜索框中输入搜索内容,并点击搜索图标
driver.findElement(By.cssSelector(".search_text")).sendKeys(new CharSequence[]{"java"});
driver.findElement(By.cssSelector(".submit_search")).click();
//元素是否被选择
//WebElement checkbox = driver.findElement(By.id("checkbox_id"));
//checkbox.isSelected();//会有返回值,如果勾选了。返回true,如果没有勾选返回false。
//元素是否启用
//WebElement enableEle = driver.findElement(By.id("loginBtn"));
//enableEle.isEnabled();
//提交表单的内容
//<button class="btn btn-major" id="loginBtn" type="submit">登录</button>
//WebElement submitEle = driver.findElement(By.id("submitBtn"));
//submitEle.submit();
//如果元素是处于iframe中,需要先切入到iframe里再定位具体的元素
//driver.switchTo().frame("id");//传入的是iframe的id
//driver.switchTo().defaultContent();//切入到默认的iframe
//操作下拉框select
/*WebElement element_province = driver.findElement(By.id("province"));
Select province = new Select(element_province);
province.selectByIndex(0); //根据所选值的位置来选择下拉框值,从0开始
province.selectByValue("22");//根据value值来选择下拉框的值
province .selectByVisibleText("北京");//根据可见文本来操作下拉菜单*/
}catch(Exception e){
e.printStackTrace();
}
}
//模拟实际鼠标的移动操作执行自动化测试
public void testAction(){
driver.get("https://www.baidu.com/");
By inputText = By.id("kw");
By searchButton = By.id("su");
//实例化action对象
Actions action = new Actions(driver);
CharSequence contents[] = { "java"};
//通过action模拟键盘输入java关键字到 输入框,只有使用了perform方法才会输入进去
action.sendKeys(driver.findElement(inputText), contents).perform();
//鼠标模拟移动到搜索按钮
action.moveToElement(driver.findElement(searchButton)).perform();
//模拟点击操作
action.click().perform();
}
}
相关推荐
java+selenium+maven+testng自动化测试框架实例(实际项目)
使用Java+Maven+Selenium+TestNG+Jedis+Jenkins搭建的WebUI自动化测试框架,资源的大体介绍如下链接http://note.youdao.com/noteshare?id=dc564343fd126f497074f6d7560c9f5e&sub=387EB3B1BAC945CEA71A5BDBC6484473
个人觉得这个工具比任何自动化测试框架都好使,使用关键字和数据双驱动,不需要写一行代码,无需维护脚本,只需要维护用例数据。Web元素只需要在Chrome中复制xPath即可,定位非常高效。 工具安全无木马,目的只为...
基于Dagger+Java ScriptEngine+Java Selenium的自动化测试框架
selenium3+java+Maven+TestNG+ReportNG+Excel自动化参数测试框架源码
1、支持多并行测试 2、支持所有的云解决方案,如 BrowserStack,SauceLabs,Testingbot 3、数据驱动测试 4、一步级报告生成 5、自动屏幕快照的失败的测试步骤 ...8、自动重新运行失败的测试用例...
采用Maven+Selenium+TestNG+TestNG-xslt实现数据驱动自动化测试框架的实现,思想主要是采用PO模式进行代码封装以及对数据文件的读取使用。
基于python+pytest+Selenium+allure,完成web自动化测试框架的搭建,并可以在jenkins上持续集成,应用于课堂派web自动化测试项目中,取得良好效果 软件开发设计:应用软件开发、系统软件开发、移动应用开发、网站开发...
Selenium自动化测试框架第1讲_Java开发环境搭建、入门2
1.seleRrj是工程包,包含简单的自动化...2.selenium-fireFox插件,安装在火狐浏览器里面 用于录制自动化脚本和学习自动化测试; 3.我用的火狐版本是v21.0版本,可以做到完美兼容;高版本几乎不能用,各位可自行调试;
#框架介绍 selenium+java+Maven-TestNG+reportNG+XML • 使用Java作为项目编程语言 • 使用Maven作为项目类型,方便管理架包 • 使用TestNG作为项目运行框架,方便执行测试用例,生成测试报告 • 使用Xml作为...
在网上或者测试行业现阶段分析了一下,python这门语言学习起来简单一些但是功能强大,最后2016年下半年决定在空余时间学习研究python+selenium,最终在年底开发出来一个可使用的基于web页面的自动化测试框架。...
Java自动化测试框架
这是一个WebUI自动化测试框架,由webdriver中文社区创办人土豆(本人技术笔名)所创建,该web自动化测试框架是用java语言编写的,基于selenium webdriver 的开源自动化测试框架,该框架结合了testng,selenium,webdriver...
Java selenium best(自己写的一套基于java的selenium自动化测试框架).zip
终极自动化测试环境搭建:Selenium+Eclipse+Junit+TestNG+Python