java实践:05_webdriver自动化测试界面

一、访问界面,手工测试

  1. 输入访问地址
  2. 测试界面功能

二、写自动化测试用例

  1. 整理测试功能点
  2. 编写测试用例

三、写自动化测试程序

  1. 浏览器
  2. 浏览器驱动
  3. 第三方自动化jar包
  4. 使用第三方jar包,写测试代码
java实践:05_webdriver自动化测试界面_第1张图片
image.png
java实践:05_webdriver自动化测试界面_第2张图片
image.png
java实践:05_webdriver自动化测试界面_第3张图片
image.png
java实践:05_webdriver自动化测试界面_第4张图片
image.png
        
            org.seleniumhq.selenium
            selenium-java
            2.25.0
        
package com.guoyasoft;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;

/**
 * this method is for deal with base web elements
 * 
 * @author Young
 *
 */
public class HtmlElement {
    public static int delayTime=1;
    public static void main(String[] args) throws Exception {
        //设置浏览器驱动
        String chromdriver="C:/software/chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", chromdriver);
        
        //设置浏览器配置和启动浏览器
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");
        WebDriver driver=new ChromeDriver(options);
        
        //打开指定界面
        String URL = "file:///Z:/12_%E6%B5%8B%E8%AF%95%E5%9F%B9%E8%AE%AD/98_%E5%9F%BA%E7%A1%80_%E4%B8%AD%E7%BA%A7_%E8%9E%8D%E5%90%88%E8%AF%BE%E4%BB%B6/02_HTTP%E5%8D%8F%E8%AE%AE/01_html%E7%95%8C%E9%9D%A2/html_00_summary.html";
        driver.get(URL);
        
        //设置界面加载等待时间
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);

        //driver.findElement()查找元素
        WebElement upload = driver.findElement(By
                .xpath("//td/input[@type='file']"));
        upload.sendKeys("C:/softwareData/workspace/Selenium/testng.xml");
        Assert.assertTrue(upload.getAttribute("value").contains("testng.xml"));
        // for alert
        WebElement clickOnAlert = driver.findElement(By
                .xpath("//td/input[@name='alterbutton']"));
        clickOnAlert.click();
        delay(delayTime);
        // get alert
        Alert alert = driver.switchTo().alert();
        Assert.assertTrue(alert.getText().contains("alert"));
        // click alert ok
        alert.accept();

        delay(delayTime);
        // for prompt
        WebElement clickOnPrompt = driver.findElement(By
                .xpath("//td/input[@name='promptbutton']"));
        clickOnPrompt.click();
        delay(delayTime);
        Alert prompt = driver.switchTo().alert();
        delay(delayTime);
        prompt.sendKeys("I love Selenium");
        prompt.accept();
        delay(delayTime);
        Alert afterAccept = driver.switchTo().alert();
        Assert.assertTrue(afterAccept.getText().contains("I love Selenium"));
        // click alert ok
        afterAccept.accept();
        delay(delayTime);
        // for confirm
        WebElement clickOnConfirm = driver.findElement(By
                .xpath("//td/input[@name='confirmbutton']"));
        clickOnConfirm.click();
        delay(delayTime);
        Alert confirm = driver.switchTo().alert();
        confirm.dismiss();
        delay(delayTime);
        Alert afterDismiss = driver.switchTo().alert();
        Assert.assertTrue(afterDismiss.getText().contains("You pressed Cancel"));
        delay(delayTime);
        afterDismiss.accept();
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

        WebElement selector = driver.findElement(By.id("Selector"));
        Select select = new Select(selector);
        delay(delayTime);
        select.selectByIndex(3);
        delay(delayTime);
        select.selectByVisibleText("桃子");
        delay(delayTime);
        select.selectByValue("apple");
        delay(delayTime);
        System.out.println(select.getAllSelectedOptions().toString());
        delay(delayTime);
        WebElement showSelectResult = driver.findElement(By
                .name("showSelectResult"));
        showSelectResult.click();
        delay(delayTime);
        Alert yourSelect = driver.switchTo().alert();
        Assert.assertTrue(yourSelect.getText().contains("苹果"));

        delay(delayTime);
        yourSelect.accept();

        // input box
        WebElement editBox = driver.findElement(By
                .xpath("//td/input[@id='edit']"));
        editBox.sendKeys("Selenium is good");
        WebElement submitButton = driver.findElement(By
                .xpath("//input[@type='button' and @name='submit']"));
        submitButton.click();
        delay(delayTime);
        Alert submitAlert = driver.switchTo().alert();
        Assert.assertEquals(submitAlert.getText(), "Selenium is good");
        submitAlert.accept();
        delay(delayTime);

        // for radio Box

        WebElement duRadioBox = driver.findElement(By
                .cssSelector("div#radio>input.Baidu"));
        WebElement aLiRadioBox = driver.findElement(By
                .cssSelector("div#radio>input.Alibaba"));
        WebElement TXRadioBox = driver.findElement(By
                .cssSelector("div#radio>input.Tencent"));
        WebElement MiRadioBox = driver.findElement(By
                .cssSelector("div#radio>input.Mi"));
        delay(delayTime);
        Assert.assertTrue(TXRadioBox.isSelected());
        Assert.assertTrue(!MiRadioBox.isEnabled());
        delay(delayTime);
        duRadioBox.click();
        Assert.assertTrue(duRadioBox.isSelected());
        delay(delayTime);

        aLiRadioBox.click();
        Assert.assertTrue(aLiRadioBox.isSelected());
        delay(delayTime);

        // for checkBox

        List webCheckBox = driver.findElements(By
                .xpath("//input[@type='checkbox']"));

        for (WebElement e : webCheckBox) {
            e.click();
            Assert.assertTrue(e.isSelected());
            delay(delayTime);
        }

        // for links
        String defaultWindow = driver.getWindowHandle();
        WebElement myLink = driver.findElement(By
                .linkText("Copyright 2017 guoyasoft"));
        delay(delayTime);
        // scroll to mylink
        JavascriptExecutor scroll = (JavascriptExecutor) driver;
        scroll.executeScript("arguments[0].scrollIntoView();", myLink);
        // open link in a new windows press shift when you click
        delay(delayTime);
        Actions actions = new Actions(driver);
        actions.keyDown(Keys.SHIFT).click(myLink).perform();

        delay(delayTime);
        Set currentWindows = driver.getWindowHandles();
        System.out.println(currentWindows.size());
        for (String window : currentWindows) {
            if (window.endsWith(defaultWindow)) {
                driver = driver.switchTo().window(window);
                driver.manage().timeouts()
                        .pageLoadTimeout(60, TimeUnit.SECONDS);
                driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
                break;
            }
        }

        Assert.assertTrue(driver.getCurrentUrl().contains("html_00_summary"));
        delay(10);

        driver.quit();
    }

    /**
     * @author Young
     * @param seconds
     */
    public static void delay(int seconds) {
        try {
            Thread.sleep(seconds * 1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(java实践:05_webdriver自动化测试界面)