起初写博客是为了妹子学习,现在发现忽略了最基础的Selenium教程,所以:从本博客开始编辑 Selenium 入门知识。(学好Java)

Selenium 入门 1:(学好Java)

  录制回放,简单粗暴(如下图)。就不解释了。

  Selenium 环境搭建(学好Java)

Selenium是个 Java 的jar包,所以。必须要搭建好 Java 环境,既Path路径。例如下图

然后 Selenium jar包导入 eclipse,方法很多。只讲最简单的,

环境搭建完毕。

搭建好环境了,那么该怎么操作呢? 还记得怎么使用 hello word 么? 没错。

  举一反三。聪明的你,现在是不是只缺API了呢。

好的,搬运工开始了。

 

API(Selenium操作,纯粹搬运工)

然后就可以按照 Selenium 的API开始啦。

API 链接 http://seleniumhq.github.io/selenium/docs/api/java/index.html

1  用webdriver打开一个浏览器
    打开firefox浏览器:
            WebDriver driver = new FirefoxDriver();

打开IE浏览器
            WebDriver driver = new InternetExplorerDriver ();

打开chrome浏览器
       WebDriverdriver = new ChromeDriver();

1.2.2  最大化浏览器  
  WebDriver driver = new FirefoxDriver();
  driver.manage().window().maximize();

1.2.3 关闭浏览器 
WebDriver driver = new FirefoxDriver();
      driver.close();
      driver.quit();

1.3  打开测试页面
    driver.get("http://www.google.com");
    driver.navigate().to("http://www.baidu.com/");
      P.S.navigate方法会产生1个Navigator对象,其封装了与导航相关的一些方法,比如前进后退等

1.4  页面元素定位
Webdriver提供下面两种方法来定位页面元素,参数是By对像,最常用是By.id和By.name查找。
    findElement   定位某个元素,如果没有找到元素会抛出异常:NoSuchElementException
    findElements     定位一组元素

例如需要定位如下元素:
  <input class="input_class" type="text" name="passwd" id="passwd-id" />

By.id:
      WebElement element = driver.findElement(By.id("passwd-id"));

By.name:
      WebElement element = driver.findElement(By.name("passwd"));

By.xpath:
      WebElement element =driver.findElement(By.xpath("//input[@id='passwd-id']"));

By.className
      WebElement element = driver.findElement(By.className("input_class"));

By.cssSelector
      WebElement element = driver.findElement(By.cssSelector(".input_class"));

By.linkText:
      WebElement element = driver.findElement(By.linkText("百科"));

By.partialLinkText:
      //这个方法就是模糊查询
      WebElement element = driver.findElement(By.partialLinkText("hao"));

By.tagName:
      String test= driver.findElement(By.tagName("form")).getAttribute("name");

1.5  如何对页面元素进行操作
1.5.1 输入框(text field or textarea)
  WebElement element = driver.findElement(By.id("passwd-id"));
    element.sendKeys(“test”);//在输入框中输入内容:
    element.clear();       //将输入框清空
    element.getText();     //获取输入框的文本内容:

1.5.2下拉选择框(Select)
  Select select = new Select(driver.findElement(By.id("select"))); 
    select.selectByVisibleText(“A”);
    select.selectByValue(“1”);
    select.deselectAll();
    select.deselectByValue(“1”);
    select.deselectByVisibleText(“A”);
    select.getAllSelectedOptions();
    select.getFirstSelectedOption();

1.5.3单选项(Radio Button)
  WebElement radio=driver.findElement(By.id("BookMode"));
    radio.click();       //选择某个单选项
    radio.clear();      //清空某个单选项
    radio.isSelected();  //判断某个单选项是否已经被选择

1.5.4多选项(checkbox)
  WebElement checkbox = driver.findElement(By.id("myCheckbox."));
    checkbox.click();
    checkbox.clear();
    checkbox.isSelected();
    checkbox.isEnabled();

1.5.5按钮(button)
WebElement btn= driver.findElement(By.id("save"));
    btn.click();      //点击按钮
    btn.isEnabled ();  //判断按钮是否enable

1.5.7弹出对话框(Popup dialogs)
  Alert alert = driver.switchTo().alert();
    alert.accept();  //确定
    alert.dismiss();  //取消
    alert.getText(); //获取文本

1.5.8表单(Form)
  Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交可以:
  WebElement approve = driver.findElement(By.id("approve"));
  approve.click();

  approve.submit();//只适合于表单的提交

1.5.9上传文件
  WebElement adFileUpload =driver.findElement(By.id("WAP-upload"));
  String filePath = "C:\test\\uploadfile\\media_ads\\test.jpg";
  adFileUpload.sendKeys(filePath);

1.6  Windows 和 Frames之间的切换
    driver.switchTo().defaultContent();     //返回到最顶层的frame/iframe
    driver.switchTo().frame("leftFrame");    //切换到某个frame:
    driver.switchTo().window("windowName"); //切换到某个window

1.7  调用Java Script
  JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("JS脚本");

1.8  超时设置
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);      //识别元素时的超时时间
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);  //页面加载时的超时时间
        driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);  //异步脚本的超时时间

自由等待(隐形等待)

  new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='btnAddEnlistAddorUpdate_relType']")));

最后,该怎么断言呢?

  断言就是用上面的 API给的 getxxx() 的方法,拿到实际值。然后断言是否等于case设定的预期。

最常用的是 element.getText(); 获取文本,判断文本内容。

  1. Assert.assertTrue(字符串.contains(预期));
  2. Assert.assertEquals(实际值 , 期望值);

By: Jack.Zhong

记得点赞哦

Selenium自动化-入门1的更多相关文章

  1. Selenium 2 入门

    在多个浏览器中进行 Web 应用程序的端到端功能测试 Selenium 是一款有名的 Web 应用程序测试框架,用于进行功能测试.新版本 Selenium 2 结合了 Selenium 1 和 Web ...

  2. 打个响指Selenium自动化开启

    最近斗哥在朋友的影响下,接触了自动化测试工具中的一个项目:appium自动化测试脚本. appium类库封装了标准Selenium客户端类库,为用户提供所有常见的JSON格式selenium命令以及额 ...

  3. selenium webdriver入门

    写在前面:最近在研究UI自动化测试的过程中,发现公司里通常用的是AutomanX框架,而这个框架实际上是基于selenium webdriver框架的,所以在编写测试用例时,很多语法都是直接使用sel ...

  4. Python+Selenium基础入门及实践

    Python+Selenium基础入门及实践 32018.08.29 11:21:52字数 3220阅读 23422 一.Selenium+Python环境搭建及配置 1.1 selenium 介绍 ...

  5. RobotFramework + Python 自动化入门 三 (Web自动化)

    在<RobotFramwork + Python 自动化入门 一>中,完成了一个Robot环境搭建及测试脚本的创建和执行. 在<RobotFramwork + Python 自动化入 ...

  6. Python2.6.6执行selenium自动化

    系统类型: [root@bogon home]# uname -aLinux bogon 2.6.32-431.el6.x86_64 #1 SMP Sun Nov 10 22:19:54 EST 20 ...

  7. Selenium自动化中DOM,XPATH,CSS定位Web页面对象的优劣性分析

    加速IE浏览器自动化执行效率:Selenium自动化中DOM,XPATH,CSS定位Web页面对象的优劣性分析 1.技术背景       在Web应用中,用户通过键盘在输入框中输入值和鼠标点击按钮,链 ...

  8. Selenium2学习-036-WebUI自动化实战实例-034-JavaScript 在 Selenium 自动化中的应用实例之六(获取 JS 执行结果返回值)

    Selenium 获取 JavaScript 返回值非常简单,只需要在 js 脚本中将需要返回的数据 return 就可以,然后通过方法返回 js 的执行结果,方法源码如下所示: /** * Get ...

  9. Selenium2学习-032-WebUI自动化实战实例-030-JavaScript 在 Selenium 自动化中的应用实例之五(高亮标示元素)

    在自动化脚本编写过程中,操作元素之前,需要对元素进行高亮显示(通过修改元素的边框样式),同时进行截图,以记录操作的元素对象.在实际应用中较为少见,通常用于演示,或者发生错误时的屏幕截图捕捉,用于错误报 ...

随机推荐

  1. fremarker导出word list

    import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java ...

  2. Spring相关问题

    1.什么是 Spring 框架?Spring 框架有哪些主要模块?Spring 框架是一个为 Java 应用程序的开发提供了综合.广泛的基础性支持的 Java 平台.Spring帮助开发者解决了开发中 ...

  3. SUSE12Sp3安装配置.net core 生产环境-总汇(持续更新中...)

    最近正在使用SUSE系统,项目环境是没有外网的,所以提供的基本都是离线安装,对应的安装包可能需要自行去下载,我这边就不整理了. 网上查找SUSE的资料比较少,于是整理了一下,希望对有需要的人有一点点帮 ...

  4. [Swift]LeetCode403. 青蛙过河 | Frog Jump

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

  5. EPPlusHelper

    public class EPPlusExcelHelper : IDisposable { public ExcelPackage ExcelPackage { get; private set; ...

  6. HBase之CF持久化系列(续1)

    这一节本来打算讲解HRegion的初始化过程中一些比较复杂的流程.不过,考虑前面的博文做的铺垫并不够,因此,在这一节,我还是特意来介绍HBase的CF持久化.关于这个话题的整体流程性分析在博文< ...

  7. 一步一步用Canvas写一个贪吃蛇

    之前在慕课网看了几集Canvas的视频,一直想着写点东西练练手.感觉贪吃蛇算是比较简单的了,当年大学的时候还写过C语言字符版的,没想到还是遇到了很多问题. 最终效果如下(图太大的话 时间太长 录制gi ...

  8. 网络协议 11 - Socket 编程(下):眼见为实耳听为虚

    系列文章传送门: 网络协议 1 - 概述 网络协议 2 - IP 是怎么来,又是怎么没的? 网络协议 3 - 从物理层到 MAC 层 网络协议 4 - 交换机与 VLAN:办公室太复杂,我要回学校 网 ...

  9. SQL注入: with rollup特性

    题目名称:因缺思汀的绕过 题目地址:http://www.shiyanbar.com/ctf/1940 1.with rollup: with rollup关键字会在所有记录的最后加上一条记录,该记录 ...

  10. 【java设计模式】(5)---装饰者模式(案例解析)

    设计模式之装饰者模式 一.概念 1.什么是装饰者模式 装饰模式是在不使用继承和不改变原类文件的情况下,动态的扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象.    这一个解释 ...