前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Katalon + 傻瓜 == selenium 代码

Katalon + 傻瓜 == selenium 代码

作者头像
小歪
发布2018-07-25 15:38:49
9760
发布2018-07-25 15:38:49
举报

、简直是神器啊 (๑• . •๑)今天在翻莫烦大大的博客时,看到他提到一个工具,便去看了下,第一感受是,太好用了、爱不释手。

下面来说说这个工具: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的代码

代码语言:javascript
复制
# -*- 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,不熟悉的可以来看看文档

代码语言:javascript
复制
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 | ]],这一步必须自己手动来操作,

代码语言:javascript
复制
driver.current_window_handle 获取当前窗口handle
driver.window_handles 获取所有窗口的handle,返回list列表
driver.switch_to.window(handle) 切换到对应的窗口
driver.close() 关闭当前窗口

测试打开多窗口的代码

代码语言:javascript
复制
# -*- 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,他们主要是提供测试工具,感兴趣的可以了解下

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-06-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python爬虫与算法进阶 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Katalon Recorder
  • 安装方法
  • 感受
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档