项目创建

环境:selenium3+chrome110版本

1.创建maven项目

 

代码写在test目录下

 

 

 

依赖:

复制代码
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.testing</groupId>
  <artifactId>testNGPO</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>testNGPO</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.10</version>
    </dependency>

    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>3.141.59</version>
    </dependency>

    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-api</artifactId>
      <version>3.141.59</version>
    </dependency>
  </dependencies>
</project>
复制代码

 

 测试代码:

复制代码
package com.testing.mall;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class ReqPub {
    private WebDriver driver;

    @BeforeClass
    public void before(){
        System.setProperty("webdriver.chrome.driver", "D:\\test\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://xxxx");
        //获取弹窗
        Alert a = driver.switchTo().alert();
        a.sendKeys("111");
        //点击确定按钮
        a.accept();
        //隐式等待5s
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    }

    @Test
    public void login() {

        driver.findElement(By.xpath("//*[@id=\"app\"]/div[1]/div[2]/div[2]/div/form/span[2]/input")).sendKeys("13311112222");
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[1]/div[2]/div[2]/div/form/span[4]/input")).sendKeys("123456");
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[1]/div[2]/div[2]/div/form/button")).click();

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

}
复制代码

 

注:

将chromedriver.exe放入到C:\Windows\System32目录下,可以不用设置环境变量了

(System.setProperty("webdriver.chrome.driver", "D:\\test\\chromedriver.exe");)

posted @   iTao0128  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示