美文网首页
Selenium Testing 1 - java

Selenium Testing 1 - java

作者: 小眼睛的露鹿酱 | 来源:发表于2019-03-21 15:24 被阅读0次

安装(java, eclipse, selenium web Driver)

假设已经成功安装java以及eclipse

  1. 下载selenium WebDriver jar包 > Downlaod >
  2. 找到如下的图, 然后去选择自己需要的版本下载, 注意 Javadoc 可以自己收藏了以后自己供自己检索使用。



    下载后直接解压到本地, 复制其中的所有jar包到eclipse使用的lib文件中

  3. 在selenium WebDriver 中不同的浏览器需要使用不一样的驱动工具, 可以放到项目的文件夹中。注意先安装chrome, 然后去看自己安装的chrome的版本, 不同的版本去下载对应的chromeDriver
    注意 去不同的Driver 网站去下载不同的driver; 例如chromewDriver就去网站下载
    selenuim webdriver
  4. 新建eclipse project, 导入需要的第三方jar包。开始以下的编程
    注意driver.close()是关闭浏览器
    system.exist()是关闭java程序, 请用在driver.close之后, 否则浏览器即使打开 java也会关闭。
  5. 下载相关的driver,例如chrome ,需要查看本地的chrome的版本, 然后下载对应的driver 链接
 System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
        WebDriver driver = new FirefoxDriver();
        String baseUrl = "http://www.facebook.com";
        String tagName = "";
        
        driver.get(baseUrl);
        tagName = driver.findElement(By.id("email")).getTagName();
        System.out.println(tagName);
        driver.close();
        System.exit(0);

加载GUI Element

使用- findElement(By.locator()) 其中使用不同的By来定位不一样的element


By类型

注意当使用By.cssSelector的时候 该函数不支持contain 特征。
** WebDriver does not support the "contains" keyword when used in the By.cssSelector() method. **

常用的类型以及函数

  1. WebElement 页面上element实例化, 其中的属性包含click, size...
 WebElement element1= driver.findElement(By.xpath("//*[@id=\"login_form\"]/table/tbody/tr[3]/td[2]/div/a"));
        WebElement element2 = driver.findElement(By.linkText("忘记帐户?"));
       System.out.println(element1.getText());
       System.out.println(element2.toString());
  1. get 相关的函数: 获取title, url等
  2. Navigate相关的函数: 打开新的窗口, 页面前进后退等
  3. close quit函数: 关闭浏览器目前打开的窗口,关闭全部
    如果存在popup页面, close()函数仅仅关闭母页面, popup页面不会被关闭, 但是如果使用quit就会关闭所有页面
  4. Switch 用语不同的窗口(frame),popup窗口
    frame: driver.switchTo().frame("classFrame");
    popup: driver.switchTo().alert()
driver.get("http://demo.guru99.com/selenium/deprecated.html");
            driver.switchTo().frame("classFrame");
            driver.findElement(By.linkText("Deprecated")).click();
            driver.close(); 
driver.get("http://jsbin.com/usidix/1");
        driver.findElement(By.cssSelector("input[value=\"Go!\"]")).click();
        alertMessage = driver.switchTo().alert().getText();
        driver.switchTo().alert().accept();
  1. wait
    显式 Explicit wait: 对特定的脚本设置等待时间
    隐式 Implicit wait: 对多有脚本都设置一样的默认时间
    导入不同的包, 去设置
  2. 条件
    使用isEnabled(), isDisplayed(),isSelected()去检测是否某些元素符合条件

Summary

To start using the WebDriver API, you must import at least these two packages.
org.openqa.selenium.*
org.openqa.selenium.firefox.FirefoxDriver
The get() method is the equivalent of Selenium IDE's "open" command.
Locating elements in WebDriver is done by using the findElement() method.
The following are the available options for locating elements in WebDriver:
By.className
By.cssSelector
By.id
By.linkText
By.name
By.partialLinkText
By.tagName
By.xpath
The By.cssSelector() does not support the "contains" feature.
You can instantiate an element using the WebElement class.
Clicking on an element is done by using the click() method.
WebDriver provides these useful get commands:
get()
getTitle()
getPageSource()
getCurrentUrl()
getText()
WebDriver provides these useful navigation commands
navigate().forward()
navigate().back()
navigate().to()
navigate().refresh()
The close() and quit() methods are used to close browser windows. Close() is used to close a single window; while quit() is used to close all windows associated to the parent window that the WebDriver object was controlling.
The switchTo().frame() and switchTo().alert() methods are used to direct WebDriver's focus onto a frame or alert, respectively.
Implicit waits are used to set the waiting time throughout the program, while explicit waits are used only on specific portions.
You can use the isEnabled(), isDisplayed(),isSelected(), and a combination of WebDriverWait and ExpectedConditions methods when verifying the state of an element. However, they do not verify if the element exists.
When isEnabled(), isDisplayed(),or isSelected() was called while the element was not existing, WebDriver will throw a NoSuchElementException.
When WebDriverWait and ExpectedConditions methods were called while the element was not existing, WebDriver would throw a TimeoutException. 

Note:

driver.get() : It's used to go to the particular website , But it doesn't maintain the browser History and cookies so , we can't use forward and backward button , if we click on that , page will not get schedule

driver.navigate() : it's used to go to the particular website , but it maintains the browser history and cookies, so we can use forward and backward button to navigate between the pages during the coding of Testcase

相关文章

网友评论

      本文标题:Selenium Testing 1 - java

      本文链接:https://www.haomeiwen.com/subject/mhtjuqtx.html