先來個(gè)macaca源碼地址吧!source code?
macaca都是開源的呀!?。『觅?/p>
一、java sample demo
源碼下載后,解壓
1、打開idea import project;源碼目錄如下

右鍵pom.xml->maven-->reimport

2、DesktopSampleTest.java 源碼解析
2.1在測試類中先創(chuàng)建macacaclient對象。
MacacaClient driver= new MacacaClient();
2.2*setUp()方法主要用于配置被測應(yīng)用的基礎(chǔ)信息,包含平臺版本,系統(tǒng)版本
@Before public void setUp() throws Exception {
? ? ? ? JSONObject porps = new JSONObject();
? ? ? ? porps.put("browserName", "electron");
? ? ? ? porps.put("platformName", "desktop");
? ? ? ? JSONObject desiredCapabilities = new JSONObject();
? ? ? ? desiredCapabilities.put("desiredCapabilities", porps);
? ? ? ? driver.initDriver(desiredCapabilities).setWindowSize(1280, 800)
? ? ? ? ? ? ? ? .get("https://www.baidu.com");
? ? }

更多參數(shù)設(shè)置詳見:desired-caps.html
2.3 testcase
@Test public void test_case_1() throws Exception {
? ? ? ? driver.elementById("kw").sendKeys("中文");
? ? ? ? driver.sleep(1000);
? ? ? ? driver.elementById("su").click();
? ? ? ? driver.sleep(3000);
? ? ? ? String html = driver.source();
? ? ? ? Assert.assertThat(html, containsString("<html>"));
? ? ? ? driver.elementByXPath("http://*[@id=\"kw\"]").sendKeys(" elementByXPath");
? ? ? ? driver.elementById("su").click();
? ? ? ? driver.takeScreenshot();
? ? }
api document:https://macacajs.github.io/wd.java/
2.4 teardown?
@After public void tearDown() throws Exception {
? ? ? ? driver.quit();
? ? }
@Bdfore 在測試執(zhí)行之前運(yùn)行 @After 在測試執(zhí)行之后執(zhí)行
3、demo用例執(zhí)行
3.1 啟動服務(wù)
打開命令行工具執(zhí)行 $ macaca server --verbose
3.2執(zhí)行測試
進(jìn)入項(xiàng)目目錄,打開命令行。執(zhí)行
$ mvn -s settings.xml clean install
$ mvn test
就可以看到自動執(zhí)行的例子了。
或者直接在idea里面右鍵DesktopSampleTest.java->run

二、Python sample demo
1、 sample-python
下載源碼后,解壓
2、源碼
macaca-desktop-sample.test.py
#coding:utf-8
import unittest
import time
from macaca import WebDriver
desired_caps = {
? ? 'platformName': 'desktop',
? ? 'browserName': 'electron'
}
server_url = {
? ? 'hostname': 'localhost',
? ? 'port': 3456
}
class MacacaTest(unittest.TestCase):
? ? @classmethod
? ? def setUpClass(cls):
? ? ? ? cls.driver = WebDriver(desired_caps, server_url)
? ? ? ? cls.driver.init()
? ? @classmethod
? ? def tearDownClass(cls):
? ? ? ? cls.driver.quit()
? ? def test_get_url(self):
? ? ? ? self.driver ? ? ? ? ? ? ? ? ? ? \
? ? ? ? ? .set_window_size(1280, 800) ? \
? ? ? ? ? .get('https://www.baidu.com')
? ? def test_search_macaca(self):
? ? ? ? self.driver ? ? ? ? ? ? ?\
? ? ? ? ? ? .element_by_id('kw') \
? ? ? ? ? ? .send_keys('macaca')
? ? ? ? self.driver ? ? ? ? ? ? ?\
? ? ? ? ? ? .element_by_id('su') \
? ? ? ? ? ? .click()
? ? ? ? time.sleep(3)
? ? ? ? html = self.driver.source
? ? ? ? self.assertTrue('macaca' in html)
? ? ? ? self.assertTrue(
? ? ? ? ? self.driver.element_by_css_selector_if_exists(
? ? ? ? ? ? '#head > div.head_wrapper'))
? ? ? ? self.driver ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?\
? ? ? ? ? ? .element_by_xpath_or_none('//*[@id="kw"]') \
? ? ? ? ? ? .send_keys(' elementByXPath')
? ? ? ? self.driver ? ? ? ? ? ? ?\
? ? ? ? ? ? .element_by_id('su') \
? ? ? ? ? ? .click()
? ? ? ? self.driver.take_screenshot()
if __name__ == '__main__':
? ? unittest.main()
Python API文檔:https://macacajs.github.io/wd.py/
3、用例執(zhí)行
打開命令行工具執(zhí)行 $ macaca server --verbose
進(jìn)入工程目錄,打開命令行 執(zhí)行:python3 macaca-desktop-sample.test.py