nihaoyanzi 发表于 2012-3-23 13:49:47

Selenium写的测163和Baidu高级搜索写的几个API

package manage;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;

public class Test163 extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.163.com/");
selenium.start();
selenium.setSpeed("3000");
}

@Test
public void testTest163() throws Exception {

String Notic;
String Type;
String[] Select;
String[] Value;
boolean Edit;
String Table;
String Attribute1;
String Attribute2;
String Attribute3;
String[] Attribute4;
String Title;
String Shot;
String[] WinId;
String[] WinNa;
String[] WinTit;
Shot = "D:\\work_information\\Eclipse_workSpace\\ScreenShot\\";
int i=1;

selenium.windowMaximize();
selenium.open("/");

selenium.waitForPageToLoad("30000");
selenium.captureScreenshot(Shot+(i++)+".png");
selenium.click("link=论坛");

verifyTrue(selenium.isTextPresent("新闻"));//验证是否出现“新闻”字样pass,新闻是text类型
verifyTrue(selenium.isElementPresent("//img"));//验证是否出现元素图片logo_png.png,isElementPresent 只能用链接、图片、等HTML元素
System.out.println("could verify true");
Notic = selenium.getText("//div[@id='login_before']");
System.out.println(Notic);
System.out.println("---------现在开始文本框-------------");
//verify 文本框

selenium.type("id=query", "获取文本框的值");
selenium.captureScreenshot(Shot+(i++)+".png");
Type= selenium.getValue("//input[@id='query']");
System.out.println(Type);

System.out.println("getValue返回值是string类型");
//是否可编辑
Edit = selenium.isEditable("//input[@id='query']");
if(Edit == true)
{
System.out.println("这个输入框是可编辑的");
}
else {System.out.println("这个输入框不可编辑");}
System.out.println("isEditable返回值是boolean类型");
// System.out.println("---------现在开始下拉框----------未完成---");
// selenium.click("//div/div/form/div/div");
// selenium.mouseOver("//div/div/form/div/div");
// selenium.select("id=login_select_main", "//div/div/form/div/div/ul/li");
// Value = selenium.getSelectedValues("//div/div/form/div/div");
// Select = selenium.getSelectOptions("//div/div/form/div/div");
// System.out.println(Value);
// System.out.println(Select);

//开始 关于click 的API Start to click the button or link
System.out.println("------------开始click-----------");
selenium.click("link=新闻");

System.out.println("click link successful");
Thread.sleep(1000);
selenium.captureScreenshot(Shot+(i++)+".png");
selenium.click("//div/form/div/div['@id=ydQuery']/button['@id=ydSubmit']");
System.out.println("Could click '有道'successful");
Thread.sleep(1000);

//开始多选框的API
System.out.println("------------单/多选框 (Radio/Check Box)未完成-----------");
System.out.println("------------表格(Table)-----------");
System.out.println("------------取出某单元格的值(下标从0开始)-----------");
//Table = selenium.getTable("//table[@id='login_auto_list']/thead/tr");//单元格
//Table = selenium.getTable("//div/div/form/div[@class='form-ipt']/table[@id='login_auto_list']/thead/tr.0.0");//表格undefine
//form[@id="login_form"]/div/table
//System.out.println("输出单元格的值"+Table);
System.out.println("取某元素属性");
Attribute1 = selenium.getAttribute("//img@width");
System.out.println("image的属性长是"+Attribute1);
Attribute2 = selenium.getAttribute("//div/div/form/div/div/input[@id='query']@type");
System.out.println("输入框的属性类型是:"+Attribute2);
Attribute3 = selenium.getAttribute("//div/div/form/div/div/input[@id='query']@class");
System.out.println("输入框的属性class是:"+Attribute3);
//selenium.getAttributeFromAllWindows()不会用
// Attribute4 = selenium.getAttributeFromAllWindows("//div/div/form/div/div/input[@id='query']@class");
// System.out.println("所有窗口的属性是:" +Attribute4);
Title = selenium.getTitle();
System.out.println(Title);
verifyEquals("网易",selenium.getTitle());
System.out.println("验证title是网易成功");
//取某元素的text值
//验证isElementPresent
verifyTrue(selenium.isElementPresent("//img"));

//验证isTextPresent
System.out.println("验证isElementPresent");
verifyTrue(selenium.isTextPresent("注册免费邮箱"));//必须是text类型
System.out.println("验证isTextPresent");

selenium.captureScreenshot(Shot+(i++)+".png");
System.out.println("截图成功");
//验证是否可见
// selenium.mouseOver("//div/div[@id='tcldivtf1b']");
// System.out.println("验证是否存在图片yt110300la_120210.swf");//图片经常换,所以注掉这部分代码
// verifyTrue(selenium.isVisible("//div/div[@id='tcldivtf1b']/embed[@src='http://img1.126.net/channel5/011092/yt110300la_120210.swf']"));
// //selenium.isVisible("//img");//失败,不可用
// System.out.println("验证展开后的图片是否存在isVisible");//图片经常换,所以注掉这部分代码
// //System.out.println("keyDown触发后不一定触发keyUp,可以用这个命令做下拉的功能");
// //selenium.keyDown("//div/div/div/form/div/div[@id='ydQuery']", "\\13");
// //selenium.keyUp("//div/div/div/form/div/div[@id='ydQuery']", "\\13");
// selenium.refresh();
// selenium.goBack();
// System.out.println("浏览器后退成功");
//JS
WinId = selenium.getAllWindowIds();
System.out.println("获取所有窗口的id是:"+WinId);
WinNa = selenium.getAllWindowNames();
System.out.println("获取所有窗口的name是:"+WinNa);
WinTit= selenium.getAllWindowTitles();
System.out.println("获取所有窗口的title是:"+WinTit);
System.out.println("怎么输出的编号都这么奇怪呢?");

}


@After
public void tearDown() throws Exception {
selenium.stop();
checkForVerificationErrors();
}
}







package manage;


import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;

public class Temp2 extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.baidu.com/");
selenium.start();
selenium.setSpeed("3000");
}

@Test
public void testTemp2() throws Exception {
String Table;
String Att;
selenium.open("/gaoji/advanced.html?q=%E7%99%BE%E5%BA%A6%E6%9F%A5%E8%AF%A2");
// selenium.click("id=ct_1");
// selenium.type("id=keyword", "1");
// selenium.check("id=ct_2");
selenium.type("id=keyword", "2");
selenium.select("name=rn", "label=每页显示100条");
System.out.println("1");
selenium.select("name=ft", "label=微软 Powerpoint (.ppt)");

Att= selenium.getAttribute("//img@height");
System.out.println("Att");
selenium.click("//input[@value='百度一下']");
}

@After
public void tearDown() throws Exception {
selenium.stop();
}
}

jia8162 发表于 2012-4-11 14:21:59

代码繁琐 ,复用度不高!建议优化代码

ryugun 发表于 2012-4-17 15:12:32

线性代码。。如2L所言 复用度太差。。
结构化模块化脚本,然后再参数化数据。。

weiweixiaocao 发表于 2012-5-13 14:27:15

呵呵,不错

wenhangyuwhy 发表于 2012-5-21 09:54:07

代码繁琐~~~~~~~:L
页: [1]
查看完整版本: Selenium写的测163和Baidu高级搜索写的几个API