当前位置:   article > 正文

selenium3.0工程代码(web端UI自动化测试)_根据源代码自动生成web自动化测试代码

根据源代码自动生成web自动化测试代码

前言:selenium是目前最主流的免费、开源web端自动化测试框架。经历了selenium1、selenium2(selenium+webdrive)、selenium3,一共三个版本迭代,目前selenium2应该还有少部分人用,主要用selenium3。因为最新版本的浏览器,比如谷歌、火狐已经不支持selenium2了。

 

1、工程目录

2、KeyWord类代码(封装seleniumAPI)

  1. import org.apache.commons.io.FileUtils;
  2. import org.openqa.selenium.*;
  3. import org.openqa.selenium.NoSuchElementException;
  4. import org.openqa.selenium.chrome.ChromeDriver;
  5. import org.openqa.selenium.firefox.FirefoxDriver;
  6. import org.openqa.selenium.interactions.Actions;
  7. import org.openqa.selenium.safari.SafariDriver;
  8. import org.openqa.selenium.support.ui.Select;
  9. import org.testng.Assert;
  10. import java.awt.*;
  11. import java.awt.datatransfer.StringSelection;
  12. import java.awt.event.KeyEvent;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.util.*;
  16. import java.util.List;
  17. class KeyWord {
  18. static WebDriver driver;
  19. private static int max_sleep_total = 20; // 超时最大值
  20. // 打开浏览器
  21. static void openBrowser(String browserName) {
  22. try {
  23. switch (browserName) {
  24. case "chrome":
  25. System.setProperty("webdriver.chrome.driver", Constants.Path_chrome);
  26. driver = new ChromeDriver();
  27. // driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //隐式等待
  28. System.out.println("打开谷歌浏览器");
  29. break;
  30. case "firefox":
  31. if (FileTools.getPropertyOsName().contains("Mac")) {
  32. System.setProperty("webdriver.firefox.bin", Constants.Path_firefox);
  33. } else if (FileTools.getPropertyOsName().contains("Windows")) {
  34. System.setProperty("webdriver.gecko.driver", Constants.Path_firefox);
  35. }
  36. driver = new FirefoxDriver();
  37. //driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS); //隐式等待,全局生效,调用findElement方法时没有立刻找到定位元素,程序会每隔一定时间不断尝试判断页面的DOM中是否出现被查找元素,超过设定的等待时间依旧没有找到,则抛出异常:NoSuchfindElementException,代码会继续往下执行
  38. System.out.println("打开火狐浏览器");
  39. break;
  40. case "safari":
  41. driver = new SafariDriver();
  42. //driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS); //隐式等待
  43. System.out.println("打开safari浏览器");
  44. break;
  45. default:
  46. RunExcelCase.judge = "fail";
  47. System.out.println("没有匹配到浏览器");
  48. snapshot("打开浏览器失败");
  49. break;
  50. }
  51. } catch (Exception e) {
  52. RunExcelCase.judge = "fail";
  53. snapshot("打开浏览器失败");
  54. }
  55. }
  56. // 浏览器最大化
  57. static void maxMize() {
  58. driver.manage().window().maximize();
  59. System.out.println("浏览器最大化");
  60. }
  61. // 打开网址
  62. static void gotoUrl(String url) {
  63. try {
  64. sleep(2000);
  65. driver.get(url);
  66. System.out.println("访问网址" + url);
  67. } catch (Exception e) {
  68. RunExcelCase.judge = "fail";
  69. snapshot("打开网址失败");
  70. }
  71. }
  72. //清除元素框内容
  73. static void clear(By by) {
  74. boolean Location = false;
  75. int sleep_total = 0;
  76. while ((!Location) && (sleep_total <= max_sleep_total)) {
  77. try {
  78. driver.findElement(by).clear();
  79. System.out.println("清除框内容: " + by + "成功");
  80. Location = true;
  81. } catch (Exception e) {
  82. System.out.println("清除框内容: " + by + "失败");
  83. sleep(2000);
  84. sleep_total += 2;
  85. System.out.println("总共已等待 " + sleep_total + " 秒");
  86. }
  87. }
  88. if (!Location) {
  89. RunExcelCase.judge = "fail";
  90. System.out.println("最终清除框" + by + "失败");
  91. snapshot("清除框内容失败:" + by);
  92. }
  93. }
  94. //根据xpath,在元素框内输入内容
  95. static void input(By by, String string) {
  96. boolean Location = false;
  97. int sleep_total = 0;
  98. while ((!Location) && (sleep_total <= max_sleep_total)) {
  99. try {
  100. driver.findElement(by).clear();
  101. System.out.println("清除框内容: " + by + "成功");
  102. driver.findElement(by).sendKeys(string);
  103. System.out.println("在框: " + by + "输入: " + string + "成功");
  104. Location = true;
  105. } catch (Exception e) {
  106. System.out.println("在框: " + by + "输入: " + string + "时出现异常");
  107. sleep(2000);
  108. sleep_total += 2;
  109. System.out.println("总共已等待 " + sleep_total + " 秒");
  110. }
  111. }
  112. if (!Location) {
  113. RunExcelCase.judge = "fail";
  114. System.out.println("最终输入" + by + "页面元素失败:");
  115. snapshot("输入内容失败");
  116. }
  117. }
  118. //单击方法
  119. static void click(By by) {
  120. boolean Location = false;
  121. int sleep_total = 0;
  122. while ((!Location) && (sleep_total <= max_sleep_total)) {
  123. try {
  124. driver.findElement(by).click();
  125. Location = true;
  126. System.out.println("单击成功:" + by);
  127. } catch (Exception e) {
  128. System.out.println("单击成功:" + by);
  129. sleep(2000);
  130. sleep_total += 2;
  131. System.out.println("总共已等待 " + sleep_total + " 秒");
  132. }
  133. }
  134. if (!Location) {
  135. RunExcelCase.judge = "fail";
  136. System.out.println("最终单击失败:" + by);
  137. snapshot("点击失败");
  138. }
  139. }
  140. // 双击方法
  141. static void doubleClick(By by) {
  142. boolean Location = false;
  143. int sleep_total = 0;
  144. while ((!Location) && (sleep_total <= max_sleep_total)) {
  145. try {
  146. WebElement inputBox = driver.findElement(by);
  147. Actions builder = new Actions(driver);// 声明Action对象
  148. builder.doubleClick(inputBox).build().perform();// 使用doubleClick方法在元素上进行双击操作
  149. Location = true;
  150. System.out.println("双击成功:" + by);
  151. } catch (Exception e) {
  152. System.out.println("双击失败:" + by + e.getMessage());
  153. sleep(2000);
  154. sleep_total += 2;
  155. System.out.println("总共已等待 " + sleep_total + " 秒");
  156. }
  157. }
  158. if (!Location) {
  159. RunExcelCase.judge = "fail";
  160. System.out.println("最终双击失败" + by);
  161. snapshot("双击失败");
  162. }
  163. }
  164. // 关闭浏览器进程
  165. static void close_browser() {
  166. try {
  167. driver.quit();
  168. System.out.println("关闭浏览器窗口 ");
  169. } catch (Exception e) {
  170. RunExcelCase.judge = "fail";
  171. System.out.println("关闭浏览器时出现异常: " + e.getMessage());
  172. snapshot("关闭浏览器失败");
  173. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/405444
推荐阅读
相关标签
  

闽ICP备14008679号