先来个macaca源码地址吧!source code
macaca都是开源的呀!!!好赞
一、java sample demo
源码下载后,解压
1、打开idea import project;源码目录如下
source code右键pom.xml->maven-->reimport
macaca2、DesktopSampleTest.java 源码解析
2.1在测试类中先创建macacaclient对象。
MacacaClient driver= new MacacaClient();
2.2*setUp()方法主要用于配置被测应用的基础信息,包含平台版本,系统版本
常见参数@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");
}
更多参数设置详见: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("//*[@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 在测试执行之前运行 @After 在测试执行之后执行
3、demo用例执行
3.1 启动服务
打开命令行工具执行 $ macaca server --verbose
3.2执行测试
进入项目目录,打开命令行。执行
$ mvn -s settings.xml clean install
$ mvn test
就可以看到自动执行的例子了。
或者直接在idea里面右键DesktopSampleTest.java->run
test case pass二、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、用例执行
打开命令行工具执行 $ macaca server --verbose
进入工程目录,打开命令行 执行:python3 macaca-desktop-sample.test.py
扫一扫,关注TestDev
网友评论