java+selenium webUI自动化测试入门demo

pom.xml



    
      junit
      junit
      4.11
      test
    

    
    
      org.seleniumhq.selenium
      selenium-java
      3.9.1
    
    
      junit
      junit
      RELEASE
      compile
    

  

    
    
      
        
          maven-clean-plugin
          3.0.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.7.0
        
        
          maven-surefire-plugin
          2.20.1
        
        
          maven-jar-plugin
          3.0.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  

java

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

import static org.hamcrest.core.StringContains.containsString;

public class SeleniumFirst {

    public static void main(String[] args) {
        //chromedriver服务地址,需要手动下载
        System.setProperty("webdriver.chrome.driver","E:\\chromedriver_win32\\chromedriver.exe");
        //自己本地最新的charm版本,需要添加启动参数
        ChromeOptions options = new ChromeOptions();

        options.addArguments("--no-sandbox");
        
        WebDriver driver = new ChromeDriver();
        //目标URL
        driver.get("https://www.baidu.com");
        
//        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
////
////        //存在iframe,首先需要进到iframe
////        ((JavascriptExecutor)driver).executeScript("document.getElementsByTagName(\"iframe\").item(0).setAttribute(\"id\",\"asn\")");
////        driver.switchTo().frame("asn");

        //获取标题
        String title = driver.getTitle();
        System.out.printf(title);

        //获取输入框
        WebElement element = driver.findElement(By.xpath("//input[@id='kw']"));
        System.out.println("1=="+element.getTagName());
        //清空输入框
        element.clear();
        
//      WebElement element1 = driver.findElement(By.id("kw"));
//      System.out.println("2=="+element1.getTagName());
//      element1.clear();

        //隐式等待2秒,TimeUnit.SECONDS表示单位秒
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

        //在百度的输入框中输入lao4j
        element.sendKeys("lao4j");

        //点击确定按钮
        driver.findElement(By.xpath("//input[@id='su']")).click();
        //隐式等待2秒,TimeUnit.SECONDS表示单位秒
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

        //获取搜索出来的第一条信息的文本值
        String text = driver.findElement(By.xpath("//div[@id='content_left']/div[@id='1']/h3[1]/a")).getText();

        //判断获取的信息是否包含lao4j的博客 - CSDN博客
        Assert.assertThat( text, containsString( "lao4j的博客 - CSDN博客" ));

        //关闭浏览器
        driver.close();

        // 浏览器最大化
        //driver.manage().window().maximize();

        // 设置浏览器的高度为800像素,宽度为480像素
        //driver.manage().window().setSize(new Dimension(800, 600))

        // 隐式等待
        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        // 浏览器后退
        //driver.navigate().back();

        // 浏览器前进
        //driver.navigate().forward()

    }

}

自定义header,请参考 https://www.cnblogs.com/zz0412/p/8126703.html
元素定位,鼠标事件,键盘事件请参考 https://www.jianshu.com/p/3829a9cc2d93

python 请求头
def chromeWebdriver(path=None, headless=False, mobile=True):
options = webdriver.ChromeOptions()
options.add_argument(’–test-type’)
options.add_argument(’–lang=zh_CN.UTF-8’)
options.add_argument(’–start-maximized’)
options.add_argument(’–disable-infobars’)
options.add_argument(’–disable-xss-auditor’)
options.add_argument(’–disable-popup-blocking’)
options.add_argument(’–ignore-certificate-errors’)
options.add_argument(’–no-sandbox’)
options.add_argument(’–disable-dev-shm-usage’)
options.add_argument(’–disable-gpu’)
if not path:
path = os.path.join(os.path.abspath(os.path.join(os.path.dirname(file), os.pardir)), “chromedriver.exe”)
if headless:
options.add_argument(’–headless’) # 无UI模式
if mobile:
mobile_emulation = {‘deviceName’: ‘Nexus 5’} # 手机模式
options.add_experimental_option(“mobileEmulation”, mobile_emulation)

return webdriver.Chrome(executable_path=path, chrome_options=options)

你可能感兴趣的:(selenium)