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

pom.xml


<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- selenium-java -->
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>3.9.1</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>RELEASE</version>
      <scope>compile</scope>
    </dependency>

  </dependencies>

    <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

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)
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值