- 前言

一、测试准备

  <profiles>
    <profile>
        <repositories>
            <repository>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
                <id>central</id>
                <name>bintray</name>
                <url>http://jcenter.bintray.com</url>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
                <id>central</id>
                <name>bintray-plugins</name>
                <url>http://jcenter.bintray.com</url>
            </pluginRepository>
        </pluginRepositories>
        <id>bintray</id>
    </profile>
</profiles>
<activeProfiles>
    <activeProfile>bintray</activeProfile>
</activeProfiles>
<dependency>
     <groupId>macaca.webdriver.client</groupId>
     <artifactId>macacaclient</artifactId>
     <version>1.0.2</version>
 </dependency>

二、运行测试

SampleTest.java类中是一个使用 electron 驱动的 web 测试项目

setup

test_case_1

test_case_2

三、发现问题

public void takeScreenshot() throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sessionId", driver.getSessionId());
        utils.request("GET", DriverCommand.SCREENSHOT, jsonObject);
    }

四、给源码动动刀

import java.io.FileOutputStream;
import java.io.OutputStream;
import sun.misc.BASE64Decoder;
public Object takeScreenshot() throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sessionId", driver.getSessionId());
        return utils.request("GET", DriverCommand.SCREENSHOT, jsonObject);
    }

public void saveScreenshot(String filename) throws Exception {
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Decode Base64
            byte[] b = decoder.decodeBuffer(takeScreenshot().toString());
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            // generate the image file
            OutputStream out = new FileOutputStream(filename);
            out.write(b);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }   
//saveScreenShot  
/** 
 * Save screenshot of the current page. 
 * @param fileName The absolute path of the image filename 
 * @return The currently instance of MacacaClient 
 * @throws Exception 
 */ 
public MacacaClient saveScreenshot(String fileName) throws Exception { 
    return this; 
}
@Test
public void test_case_1() throws Exception {
    System.out.println(driver.elementById("kw").getAttribute("name"));
    driver
        .elementById("kw")
        .sendKeys("macaca")
        .sleep(1000)
        .elementById("su")
        .click()
        .sleep(3000);

    String html = driver.source();

    Assert.assertThat(html, containsString("<html>"));

    driver
        .elementByCss("#head > div.head_wrapper")
        .elementByXPath("//*[@id=\"kw\"]")
        .sendKeys(" elementByXPath")
        .elementById("su")
        .click()
        .saveScreenshot("C:/1.png")
        .saveScreenshot("C:/1.jpg");
}

五、github 分支提交

$ git clone https://github.com/adfghzhang/test.git
Cloning into 'test'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
Checking connectivity... done.
$ cd test
adfgh@QIANCHANG MINGW64 ~/Desktop/test (master)
$ ls
README.md
adfgh@QIANCHANG MINGW64 ~/Desktop/test (master)
$ git checkout -b branch
$ git branch//查看分支
 - branch//当前所在分支前面有*号
  master
Switched to a new branch 'branch'
$ ls
README.md
$ vi README.md
//add something in README.md
$ git add README.md
$ git commit -m "add branch"
[branch 5d21c08] add branch
 Committer: qian chang <qian chang>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
    git config --global user.name "Your Name"
    git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
    git commit --amend --reset-author
 1 file changed, 1 insertion(+)
$ git push origin branch
Logon failed, use ctrl+c to cancel basic credential prompt.
Username for 'https://github.com/': adfghzhang@live.cn
error: unable to read askpass response from 'D:/Program Files/Git/mingw64/libexec/git-core/git-gui--askpass'
Password for 'https://adfghzhang@live.cn@github.com/':
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/adfghzhang/test.git
 - [new branch]      branch -> branch

六、一点感触


↙↙↙阅读原文可查看相关链接,并与作者交流