selenium java自动化测试

时间:2022-06-24 15:21:26

最近又有同学问我一些selenium java的问题,往往都是不知道api怎么写,其实用法什么的都和python类似,只是写法可能不一样,小编今天就给大家来发一篇。

    不多说还是上代码,没有代码没有真相。


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.*;

import org.openqa.selenium.Dimension;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.interactions.Actions;


public class TestBaidu {

public static void main(String[] args) throws InterruptedException {

System.out.println("打开火狐浏览器");

WebDriver driver = new FirefoxDriver();

Thread.sleep(3000);

System.out.println("更改浏览器分辨率为最大");

driver.manage().window().maximize();

Thread.sleep(3000);

System.out.println("更改浏览器分辨率为800x400");

driver.manage().window().setSize(new Dimension (800,400));

Thread.sleep(3000);

System.out.println("打开百度首页");

driver.get("http://www.baidu.com");

Thread.sleep(3000);

System.out.println("输入搜索内容");

driver.findElement(By.id("kw")).sendKeys("selenium");

Thread.sleep(3000);

System.out.println("点击搜索按钮");

driver.findElement(By.id("su")).click();

        Thread.sleep(3000);

        System.out.println("使用xpath进行定位");

        driver.findElement(By.xpath(".//*[@id='content_left']/div[1]/div/span/strong/a")).click();

        Thread.sleep(3000);

        System.out.println("获取当前页title和url");

        System.out.printf("当前title为: %s\n", driver.getTitle());

        System.out.printf("当前url为: %s\n", driver.getCurrentUrl());

        Thread.sleep(5000);

        System.out.println("浏览器后退, 然后获取当前页title和url");

        driver.navigate().back();

        System.out.printf("当前title为: %s\n", driver.getTitle());

        System.out.printf("当前url为: %s\n", driver.getCurrentUrl());

        Thread.sleep(5000);

        System.out.println("浏览器前进, 然后获取当前页title和url");

        driver.navigate().forward();

        System.out.printf("当前title为: %s\n", driver.getTitle());

        System.out.printf("当前url为: %s\n", driver.getCurrentUrl());

        Thread.sleep(5000);

        System.out.println("刷新操作");

        driver.navigate().refresh();

        Thread.sleep(3000);

        System.out.println("删除搜索条件并开始新的搜索");

        driver.findElement(By.id("kw")).clear();

        driver.findElement(By.id("kw")).sendKeys("来吧");

        driver.findElement(By.id("su")).click();

        Thread.sleep(3000);

        System.out.println("删除搜索条件并使用表单提交的方式开始新的搜索");

        driver.findElement(By.id("kw")).clear();

        driver.findElement(By.id("kw")).sendKeys("使用submit提交表单");

        driver.findElement(By.id("kw")).submit();

        Thread.sleep(3000);

        driver.get("http://www.baidu.com");

        Thread.sleep(2000);

        System.out.println("获取百度搜索框的大小:");

        WebElement testsize = driver.findElement(By.id("kw"));

        System.out.println(testsize.getSize());

        Thread.sleep(3000);

        WebElement testtext = driver.findElement(By.xpath(".//*[@id='dwab']"));

        System.out.println(testtext.getText());

        Thread.sleep(3000);

        WebElement attrib = driver.findElement(By.xpath(".//*[@id='dkd']"));

        System.out.println(attrib.getAttribute("id"));

        Thread.sleep(3000);

        driver.manage().timeouts().pageLoadTimeout(10,TimeUnit.SECONDS);

        WebElement photo = driver.findElement(By.xpath(".//*[@id='form']/span[1]/span"));

        System.out.println(photo.isDisplayed());

        Thread.sleep(3000);

driver.manage().timeouts().pageLoadTimeout(10,TimeUnit.SECONDS);

String sreach_handle = driver.getWindowHandle();

System.out.println(sreach_handle);

driver.findElement(By.xpath(".//*[@id='u1']/a[7]")).click();

Thread.sleep(8000);

driver.findElement(By.xpath(".//*[@id='pass_phoenix_btn']/ul/li[1]/a")).click();

Set<String> handles = driver.getWindowHandles();

for(String handle : handles){

         if (handle.equals(sreach_handle)==false){

         //切换到注册页面

         driver.switchTo().window(handle);

         System.out.printf("当前title为: %s\n", driver.getTitle());

         Thread.sleep(8000);

         driver.switchTo().frame("ptlogin_iframe");

         driver.findElement(By.xpath(".//*[@id='test']")).click();

         Thread.sleep(8000);

         }

        }

driver.switchTo().window(sreach_handle);

Actions action = new Actions(driver);

action.clickAndHold(driver.findElement(By.linkText("设置"))).perform();

Thread.sleep(1000);

       

driver.quit();

}

}


更多交流:

App自动化及Web自动化全栈测试:544126564

全国零基础测试转行群:321264545