Fork me on GitHub

Katalon + 傻瓜 == selenium

这是崔斯特的第五十六篇原创文章

简直是神器啊 (๑• . •๑)

今天在翻莫烦大大的博客时,看到他提到一个工具,便去看了下,第一感受是,太好用了、爱不释手。

下面来说说这个工具:Katalon Recorder

Katalon Recorder

安装地址:Katalon Recorder

官方介绍是:

Best Selenium IDE record, play, debug app. Exports Selenium WebDriver code. Provides reports, logs, screenshots. Fast & extensible.

简单来说,他可以记录你在浏览器上的每一个动作,包括、点击、输入、输入字符等等,最后一键转化为编程代码,可以转化的语言有:

  1. C#
  2. JAVA
  3. Katalon Studio
  4. Python2
  5. Roboot Framework
  6. Ruby
  7. XML

例如下图就是直接转化为 Python2的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class UntitledTestCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.katalon.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_untitled_test_case(self):
driver = self.driver
driver.get("https://zhangslob.github.io/")
driver.find_element_by_link_text(u"Cookies池的后续解决方案").click()
driver.find_element_by_xpath("//main[@id='main']/div/div").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()

虽然说用的是Python2,但是并不影响,简单改一下就就可以使用了,主要逻辑在test_untitled_test_case函数中,可以直接拿来使用。

注意这里使用了unittest,不熟悉的可以来看看文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()

安装方法

这里建议直接在Chrome应用市场安装,地址 Katalon Recorder

考虑到有些同学可能那啥,所以我已经下载好了。公众号:Python爬虫与算法进阶,回复:傻瓜

感受

这个相当于按键精灵,把我们对浏览器的每一步操作都完成了,可以用来生成代码,和postman一样,所以我才会说 Katalon + 傻瓜 == selenium,完全是傻瓜操作。

比如来做一些自动化登录、注册等板块会非常爽,但是验证码部分还是需要自行解决。

但是该软件也是有一些问题的,它不能进行多页面切换,也就是不能自动切换到新打开的窗口,会有错误提示# ERROR: Caught exception [ERROR: Unsupported command [selectWindow | win_ser_1 | ]],这一步必须自己手动来操作,

1
2
3
4
driver.current_window_handle 获取当前窗口handle
driver.window_handles 获取所有窗口的handle,返回list列表
driver.switch_to.window(handle) 切换到对应的窗口
driver.close() 关闭当前窗口

测试打开多窗口的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Zhihu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.katalon.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_zhihu(self):
driver = self.driver
driver.get("https://www.zhihu.com/people/cuishite/activities")
driver.find_element_by_xpath("//div[@id='ProfileMain']/div/ul/li[2]/a").click()
driver.find_element_by_xpath("//div[@id='ProfileMain']/div/ul/li[4]/a/span").click()
driver.find_element_by_link_text(u"Cookies池的后续解决方案").click()
# ERROR: Caught exception [ERROR: Unsupported command [selectWindow | win_ser_1 | ]]
driver.find_element_by_xpath("//img[contains(@src,'https://pic4.zhimg.com/v2-7ff26e52e6c82c080f62d8e9291e532b_b.jpg')]").click()
driver.find_element_by_link_text(u"Cookies池的后续解决方案").click()
# ERROR: Caught exception [ERROR: Unsupported command [selectWindow | win_ser_2 | ]]
driver.find_element_by_xpath("//div[@id='js_article']/div[2]").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()

所以只需要这一点就可以完善代码,同时可以看看他们的官网 KATALON - Best automated testing tool for web, mobile, API,他们主要是提供测试工具,感兴趣的可以了解下。