20220703 web自动化测试实战一

实战目标

  • 企业微信的用户管理

课堂ppt

PPT地址

知识汇总

汇总.zip (1.9 MB)

chromedriver

$ chromedriver
Starting ChromeDriver 98.0.4758.80 (7f0488e8ba0d8e019187c6325a16c29d9b7f4989-refs/branch-heads/4758@{#972}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

下载地址

<dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.javafaker</groupId>
            <artifactId>javafaker</artifactId>
            <version>1.0.2</version>
        </dependency>

    </dependencies>

浏览器复用的老师也辛苦补充下

麻烦老师后续把Select标签相关的知识点也加一下

package com.ceshiren;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class DemoTest {

        @Test
        void loginEnterpriseWechatTest() throws IOException {

            WebDriver webDriver = new ChromeDriver();

            webDriver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx");

            webDriver.manage().window().maximize();

            String beforeURL = webDriver.getCurrentUrl();

            ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());

            TypeReference<List<HashMap<String,Object>>> typeReference=new TypeReference<>(){};

            List<HashMap<String, Object>> cookies = objectMapper.readValue(Paths.get("cookies.yaml").toFile(), typeReference);

            List<Long> expireList = new ArrayList<>();

            cookies.forEach(cookie->{
                if(cookie.get("expiry")!=null){
                    String expireStr=cookie.get("expiry").toString();
                    Long expire=Long.valueOf(expireStr);
                    expireList.add(expire);
                }
            });

            System.out.println("获取全部的时间戳: " + expireList);

            long nowCookieTime = expireList.get(0) - 31536000000L;
            System.out.println("nowCookieTime: " + nowCookieTime);

            //当前的时间戳
            long nowTime = System.currentTimeMillis();
            
            System.out.println("nowTime: " + nowTime);

            System.out.println("当前cookie有效期剩余时间: " + (nowTime-nowCookieTime));

            if((nowTime-nowCookieTime) < 7200*1000){

                System.out.println("获取有效的cookie信息,直接登录");

                cookies.stream()
                        .filter(cookie->cookie.get("domain").toString().contains(".weixin.qq.com"))
                        .forEach(cookie-> {
                                    webDriver.manage().addCookie(new Cookie(cookie.get("name").toString(),cookie.get("value").toString()));
                                }
                        );

                webDriver.navigate().refresh();

                String afterURL = null;

                afterURL = webDriver.getCurrentUrl();

                System.out.println(!afterURL.equals(beforeURL)?"登录成功":"登录失败");

            }else{

                System.out.println("cookie失效,重新扫码登录");

                WebDriverWait wait = new WebDriverWait(webDriver, Duration.ofSeconds(30), Duration.ofSeconds(2));

                wait.until(driver->!driver.getCurrentUrl().equals(beforeURL));

                Set<Cookie> newCookies = webDriver.manage().getCookies();

                try {
                    objectMapper.writeValue(Paths.get("cookies.yaml").toFile(),newCookies);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
}

cookie复用合并及添加成员正反例作业: https://gitee.com/dongqiang1122/dongqiang-test 目录:/src/test/java/web/cookie

注意

课程直播的时候,在判断cookie的有效时间长度时,应该是7200*1000,但是直播的时候只写了7200。这个大家在实战的时候记得*1000。这样算下来才是毫秒值。7200是秒。