51Testing软件测试论坛

标题: Selenium写的测163和Baidu高级搜索写的几个API [打印本页]

作者: nihaoyanzi    时间: 2012-3-23 13:49
标题: Selenium写的测163和Baidu高级搜索写的几个API
  1. package manage;

  2. import org.junit.After;
  3. import org.junit.Before;
  4. import org.junit.Test;

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

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

  14. @Test
  15. public void testTest163() throws Exception {

  16. String Notic;
  17. String Type;
  18. String[] Select;
  19. String[] Value;
  20. boolean Edit;
  21. String Table;
  22. String Attribute1;
  23. String Attribute2;
  24. String Attribute3;
  25. String[] Attribute4;
  26. String Title;
  27. String Shot;
  28. String[] WinId;
  29. String[] WinNa;
  30. String[] WinTit;
  31. Shot = "D:\\work_information\\Eclipse_workSpace\\ScreenShot\\";
  32. int i=1;

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

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

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

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

  49. System.out.println("getValue返回值是string类型");
  50. //是否可编辑
  51. Edit = selenium.isEditable("//input[@id='query']");
  52. if(Edit == true)
  53. {
  54. System.out.println("这个输入框是可编辑的");
  55. }
  56. else {System.out.println("这个输入框不可编辑");}
  57. System.out.println("isEditable返回值是boolean类型");
  58. // System.out.println("---------现在开始下拉框----------未完成---");
  59. // selenium.click("//div/div/form/div[3]/div");
  60. // selenium.mouseOver("//div/div/form/div[3]/div");
  61. // selenium.select("id=login_select_main", "//div/div/form/div[3]/div/ul/li[12]");
  62. // Value = selenium.getSelectedValues("//div/div/form/div[3]/div");
  63. // Select = selenium.getSelectOptions("//div/div/form/div[3]/div");
  64. // System.out.println(Value);
  65. // System.out.println(Select);

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

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

  75. //开始多选框的API
  76. System.out.println("------------单/多选框 (Radio/Check Box)未完成-----------");
  77. System.out.println("------------表格(Table)-----------");
  78. System.out.println("------------取出某单元格的值(下标从0开始)-----------");
  79. //Table = selenium.getTable("//table[@id='login_auto_list']/thead/tr");//单元格
  80. //Table = selenium.getTable("//div/div/form/div[@class='form-ipt']/table[@id='login_auto_list']/thead/tr.0.0");//表格undefine
  81. //form[@id="login_form"]/div/table
  82. //System.out.println("输出单元格的值"+Table);
  83. System.out.println("取某元素属性");
  84. Attribute1 = selenium.getAttribute("//img[contains(@src,'logo_png.png')]@width");
  85. System.out.println("image的属性长是"+Attribute1);
  86. Attribute2 = selenium.getAttribute("//div/div/form/div/div/input[@id='query']@type");
  87. System.out.println("输入框的属性类型是:"+Attribute2);
  88. Attribute3 = selenium.getAttribute("//div/div/form/div/div/input[@id='query']@class");
  89. System.out.println("输入框的属性class是:"+Attribute3);
  90. //selenium.getAttributeFromAllWindows()不会用
  91. // Attribute4 = selenium.getAttributeFromAllWindows("//div/div/form/div/div/input[@id='query']@class");
  92. // System.out.println("所有窗口的属性是:" +Attribute4);
  93. Title = selenium.getTitle();
  94. System.out.println(Title);
  95. verifyEquals("网易",selenium.getTitle());
  96. System.out.println("验证title是网易成功");
  97. //取某元素的text值
  98. //验证isElementPresent
  99. verifyTrue(selenium.isElementPresent("//img[contains(@src,'logo_png.png')]"));

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

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

  126. }


  127. @After
  128. public void tearDown() throws Exception {
  129. selenium.stop();
  130. checkForVerificationErrors();
  131. }
  132. }
复制代码






复制代码
  1. package manage;


  2. import com.thoughtworks.selenium.*;
  3. import org.junit.After;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import java.util.regex.Pattern;

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

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

  26. Att= selenium.getAttribute("//img[contains(@src,'http://www.baidu.com/img/logo-yy.gif')]@height");
  27. System.out.println("Att");
  28. selenium.click("//input[@value='百度一下']");
  29. }

  30. @After
  31. public void tearDown() throws Exception {
  32. selenium.stop();
  33. }
  34. }
复制代码

作者: jia8162    时间: 2012-4-11 14:21
代码繁琐 ,复用度不高!建议优化代码
作者: ryugun    时间: 2012-4-17 15:12
线性代码。。如2L所言 复用度太差。。
结构化模块化脚本,然后再参数化数据。。
作者: weiweixiaocao    时间: 2012-5-13 14:27
呵呵,不错
作者: wenhangyuwhy    时间: 2012-5-21 09:54
代码繁琐~~~~~~~




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2