UI自动化测试之selenium(1)

时间:2022-06-09 04:55:37

标签:selenium   api   

目录

1 对浏览器操纵
1.1 用webdriver打开一个浏览器
1.2 最大化浏览器&*浏览器
1.3 设置浏览器窗口巨细
1.4 打开测试页面
1.5 措置惩罚惩罚浏览器弹出的新窗口
2 页面元素定位
3 如何对页面元素进行操纵
3.1 WebElement相关要领
3.2 iFrame的措置惩罚惩罚
3.3 输入框(text field or textarea)
3.4 下拉选择框(Select)
3.5 单选项(Radio Button)
3.6 多选项(checkbox)
3.7 按钮(button)
3.8 措置惩罚惩罚Alert
3.9 上传文件
3.9.1 元素标签是Input时上传方法
3.9.2 通过操纵桌面浏览窗口上传
3.10 Selenium措置惩罚惩罚HTML5
3.10.1 措置惩罚惩罚Vedio
3.10.2 措置惩罚惩罚Canvas
3.11 表单(Form)
4 其他
4.1 期待元素加载
4.2 执行JS脚本
4.3 模拟键盘操纵

1 对浏览器操纵

返回
1.1 用webdriver打开一个浏览器
复制代码

//打开firefox浏览器:
WebDriver driver = new FirefoxDriver();
//打开IE浏览器
WebDriver driver = new InternetExplorerDriver ();
//打开HtmlUnit浏览器
WebDriverdriver = new HtmlUnitDriver();
//打开chrome浏览器
WebDriverdriver = new ChromeDriver();

复制代码
1.2 最大化浏览器&*浏览器

WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.close();
driver.quit();

1.3 设置浏览器窗口巨细
View Code
1.4 打开测试页面

打开测试页面
driver.get("");
driver.navigate().to("");
//navigate要领会孕育产生1个Navigator东西,其封装了与导航相关的一些要领,好比前进撤退退却等

1.5 措置惩罚惩罚浏览器弹出的新窗口
View Code
2 页面元素定位

返回

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
//通俗点就是精确盘问
WebDriver driver = new FirefoxDriver();
driver.get("");
WebElement element = driver.findElement(By.linkText("百科"));
//By.partialLinkText:
//这个要领就是模糊盘问
WebDriver driver = new FirefoxDriver();
driver.get("");
WebElement element = driver.findElement(By.partialLinkText("hao"));
//By.tagName
WebDriver driver = new FirefoxDriver();
driver.get("");
String test= driver.findElement(By.tagName("form")).getAttribute("name");
System.out.println(test);

复制代码
3 如何对页面元素进行操纵

返回
3.1 WebElement相关要领
Method Summary
void clear() If this element is a text entry element, this will clear the value.
void click() Click this element.
WebElement findElement(By by) Find the first WebElement using the given method.
java.util.List<WebElement> findElement(By by) Find all elements within the current context using the given mechanism.
java.lang.String getAttribute(java.lang.String name) Get the value of a the given attribute of the element.
java.lang.String getCssValue(java.lang.String.propertyName) Get the value of a given CSS property.
Point GetLocation() Where on the page is the top left-hand corner of the rendered element?
Dimension getSize() What is the width and height of the rendered element?
java.lang.String getTagName() Get the tag name of this element.
java.lang.String getText() Get the visible (i.e. not hidden by CSS) innerText of this element, including
sub-elements, without any leading or trailing whitespace.
boolean isDisplayed() Is this element displayed or not? This method avoids the problem of having to parse an element‘s "style" attribute.
boolean isEnabled() Is the element currently enabled or not? This will generally return true for everything but disabled input elements.
boolean isSelected() Determine whether or not this element is selected or not.
void sendKeys(java.lang.CharSequence... keysToSend) Use this method to simulate typing into an element, which may set its value.
void submit() If this current element is a form, or an element within a form, then this will be submitted to the remote server.
3.2 iFrame的措置惩罚惩罚

driver.switchTo().frame(“city_set_ifr”); //传入的是iframe的ID
dr.switchTo().defaultContent(); //如果要返回到以前的默认content

3.3 输入框(text field or textarea)