1111web自动化之企业微信自动登录实战

git地址:https://github.com/zhangzjr/junit5.git

/**
* 从 cookies.yaml文件中 读取
* @throws IOException
* @throws InterruptedException
*/
@Test
void qyWeixinLoginedTest() throws IOException, InterruptedException{

    WebDriver driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.get("https://work.weixin.qq.com/wework_admin/frame");

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

    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    TypeReference typeReference=new TypeReference<List<HashMap<String, Object>>>() {};
    List<HashMap<String, Object>> cookies = (List<HashMap<String, Object>>) mapper.readValue(new File("cookies.yaml"), typeReference);
    System.out.println(cookies);

    cookies.forEach(cookieMap->{
        driver.manage().addCookie(new Cookie(cookieMap.get("name").toString(), cookieMap.get("value").toString()));
    });

    driver.navigate().refresh();

    driver.findElement(By.xpath("//span[@class='index_service_cnt_item_title']")).click();

    driver.findElement(By.name("username")).sendKeys("张三一");
    driver.findElement(By.name("acctid")).sendKeys("12345");
    driver.findElement(By.name("mobile")).sendKeys("18837159999");
    driver.findElement(By.linkText("保存并继续添加")).click();

    Thread.sleep(1000);

    //driver.findElement(By.linkText("首页")).click();
    driver.findElement(By.linkText("通讯录")).click();

}
  • 添加部门

  • 批量添加成员

https://github.com/dabaoting/wechat-web-test/tree/master/src/test/java/com.hogwarts

https://github.com/kakaclaire/SeleniumDemo/tree/master/src/test/java/WeWorkTestPom

https://github.com/shou59232765/junit5demo.git

https://gitee.com/cmd_gitee/selenium-demo.git

public class WxTestLogin {

    public static WebDriver driver;

    /*
     *登录企业微信并将cookies 写入yaml文件中
     *  */
    @Test
    public void testLogin() throws IOException, InterruptedException {
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.get("https://work.weixin.qq.com/wework_admin/frame");

        //等待20秒
        Thread.sleep(20000);
        //获取cookies
        Set<Cookie> cookies = driver.manage().getCookies();
        //创建一个yaml文件
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        //将cookies存入yaml文件中
        mapper.writeValue(new File("cookies.yaml"), cookies);


    }

    @Test
    public void testLogined() throws IOException, InterruptedException {
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.get("https://work.weixin.qq.com/wework_admin/frame");

        //读取cookie
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        TypeReference typeReference = new TypeReference<List<HashMap<String, Object>>>() {
        };
        List<HashMap<String, Object>> cookies = (List<HashMap<String, Object>>) mapper.readValue(new File("cookies.yaml"), typeReference);
        System.out.println(cookies);

        //重新设置cooki
        cookies.forEach(cookieMap -> {
                    driver.manage().addCookie(new Cookie(cookieMap.get("name").toString(), cookieMap.get("value").toString()));
                }
        );

        //刷新页面
        driver.navigate().refresh();

        driver.findElement(By.xpath("//*[@id=\"_hmt_click\"]/div[1]/div[4]/div[2]/a[1]/div/span[2]")).click();
        driver.findElement(By.id("username")).sendKeys("Wuweibin");
        driver.findElement(By.id("memberAdd_english_name")).sendKeys("weibin");
        driver.findElement(By.xpath("//*[@id=\"memberAdd_mail\"]")).sendKeys("752061536@qq.com");
        driver.findElement(By.xpath("//*[@id=\"memberAdd_acctid\"]")).sendKeys("Nb151112");
        driver.findElement(By.xpath("//*[@id=\"js_contacts408\"]/div/div[2]/div/div[4]/div/form/div[3]/a[2]")).click();


    }

    @AfterAll
    public static void closees(){
        driver.quit();
    }

}
public class WxTestLogin {
    public static WebDriver driver;
    @BeforeAll
    public static void testSearch(){
        System.setProperty("webdriver.chrome.driver","D:\\work\\WebDriver\\bin\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    }
    @Test
    public void testLogin() throws IOException, InterruptedException {
        driver.get("https://work.weixin.qq.com/wework_admin/frame");
//        Set<Cookie> cookies = driver.manage().getCookies();  //cookies后alt+enter
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
//        mapper.writeValue(new File("cookies.yaml"),cookies);
        //读取cookie
        TypeReference<List<HashMap<String, Object>>> typeReference = new TypeReference<List<HashMap<String,Object>>>(){};  //反序列化
        List<HashMap<String, Object>> cookies = mapper.readValue(new File("cookies.yaml"),typeReference);
        //重新设置cooki
        cookies.forEach(cookieMap ->{
            driver.manage().addCookie(new Cookie(cookieMap.get("name").toString(),cookieMap.get("value").toString()));
        });
        //刷新页面
        driver.navigate().refresh();

        driver.quit();
    }
}

自动登录和添加成员的作业
https://github.com/chenzhiyou/selenium/commits/master