- public class BaseElectron {
- private Logger logger = Logger.getLogger(BaseElectron.class);
- public static webDriver driver;
- @BeforeSuite
- @Parameters(value={
- "electronType","driverPath"
- })
- public void init(String electronType,String driverPath) throws Exception{
- logger.info("配置信息: ELectron 版本:["+electronType+"] , 驱动文件路径:["+driverPath+"]");
- System.setProperty("webdriver.chrome.driver",driverPath);// You can skip this if chromedriver is already included in the PATH.
- ChromeOptions options = new ChromeOptions();
- options.setBinary(electronType);// 设置二进制文件, 一定用绝对路径, 不要用 / 的写法
- DesiredCapabilities capabilities = new DesiredCapabilities();// 负责启动服务端时的参数设置
- capabilities.setCapability(ChromeOptions.CAPABILITY, options);// 将参数 options 添加到设置中
- logger.info("************* 创建了 chrome 驱动对象, 打开了 Electron, 开始测试 *****************");
- driver = new ChromeDriver(capabilities);// 欺骗 chromdriver 启动 electron
- // Now, your electron App would have been opened.
- // Now if you open the dev tools using CMD+ALT+I you would notice two dev tools and first one being for the electron shell. We need to switch to the second Windows handle. Let's do that.
- Thread.sleep(5000);
- for (String handle : driver.getWindowHandles())
- {
- System.out.println(handle);
- driver.switchTo().Windows(handle); // Since there are two Windows handles this would switch to last one(which is second one). You can also explicitly provide the Windows number.
- }
- // If you inspect using the Dev Tools, you would notice the second Windows Dev Tools corresponds to actual page you have opened.
- // From here you can write the usual selenium script and it will work.
- driver.findElement(By.xpath("//a[@ng-click='login()']")).click();
- Thread.sleep(5000);
- // 跳转后, 会生成新的窗口, 所以要跳转到最后这一个窗口, 才能找到元素
- for (String handle : driver.getWindowHandles())
- {
- System.out.println(handle);
- driver.switchTo().Windows(handle); // Since there are two Windows handles this would switch to last one(which is second one). You can also explicitly provide the Windows number.
- }
- Thread.sleep(3000);
- driver.findElement(By.xpath("//span[text()='进货']")).click();
- Thread.sleep(3000);
- driver.findElement(By.xpath("//span[text()='销售']")).click();
- Thread.sleep(3000);
- driver.findElement(By.xpath("//span[text()='库存']")).click();
- }
- @AfterSuite
- public void tearDown() throws InterruptedException{
- Thread.sleep(3000);
- logger.info("************ 测试完成, 关闭驱动对象 ***********");
- driver.quit();
- }
- /** 显示等待(智能等待)
- * @param locator
- * @return
- */
- public WebElement getElement(By locator){
- WebDriverWait wait = new WebDriverWait(driver, 30);
- try {
- WebElement webElement = wait.until(ExpectedConditions.presenceOfElementLocated(locator));
- logger.info("元素定位成功");
- return webElement;
- } catch (Exception e) {
- logger.error("元素定位失败, 超时了");
- }
- return null;
- }
- /** 根据页面关键字 + 元素关键字来获取元素
- * @param pageKeyword 页面关键字
- * @param elementKeyword 元素关键字
- * @return
- */
- public WebElement getElement(String pageKeyword,String elementKeyword){
- // 先根据页面关键字和元素关键字拿到 ui 库当中的 UIElement 对象
- UIElement uiElement = UILibraryUtil.getUIElement(pageKeyword, elementKeyword);
- // 通过拿到的 UIElement 对象, 取出 by 和 value 属性值来判断通过什么方式来定位页面的元素
- String by = uiElement.getBy();
- String value = uiElement.getValue();
- logger.info("根据 {
- by:"+by+",value:"+value+"
- } 来定位["+pageKeyword+"] 页面的["+elementKeyword+"] 元素");
- By locator = null;
- // 通过什么选择器来定位元素, 取决于你配的 by 是什么信息
- if ("id".equals(by)) {
- locator = By.id(value);
- }else if ("name".equalsIgnoreCase(by)) {
- locator = By.name(value);
- }else if ("classname".equalsIgnoreCase(by)) {
- locator = By.className(value);
- }else if ("CSSSelector".equalsIgnoreCase(by)) {
- locator = By.cssSelector(value);
- }else if ("linkText".equalsIgnoreCase(by)) {
- locator = By.linkText(value);
- }else if ("partialLinkText".equalsIgnoreCase(by)) {
- locator = By.partialLinkText(value);
- }else if ("tagName".equalsIgnoreCase(by)) {
- locator = By.tagName(value);
- }else if ("xpath".equalsIgnoreCase(by)) {
- locator = By.xpath(value);
- }
- // return driver.findElement(locator);
- return getElement(locator);
- }
- /** 自己封装的方法(判断当前页面的 URL 路径包含了我们想要看到的信息, 用了延时等待处理)
- * @param part
- * @return
- */
- public boolean urlPresenceContent(String part){
- WebDriverWait wait = new WebDriverWait(driver, 30);
- try {
- boolean flag= wait.until(ExpectedConditions.urlContains(part));
- return flag;
- } catch (Exception e) {
- System.out.println("超时了");
- }
- return false;
- }
- /** 访问测试页面
- * @param url 页面地址
- */
- public void totesturl(String url){
- logger.info("访问测试页面:["+url+"]");
- driver.navigate().to(url);
- }
- /** 回退测试页面
- *
- */
- public void back(){
- logger.info("回退测试页面");
- driver.navigate().back();
- }
- /**
- * 往输入框写入数据
- */
- public void sendKeys(WebElement element,String value){
- logger.info("写入数据:["+value+"]");
- element.sendKeys(value);
- }
来源: http://www.bubuko.com/infodetail-3036313.html