macaca(二) sample demo

先來個(gè)macaca源碼地址吧!source code?

macaca都是開源的呀!?。『觅?/p>

一、java sample demo

sample-java 源碼地址

源碼下載后,解壓

1、打開idea import project;源碼目錄如下

source code

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

macaca?

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ù)設(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

test case pass

二、Python sample demo

1、 sample-python

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



最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容