其他分享
首页 > 其他分享> > 自动化测试框架playwright对比katalon

自动化测试框架playwright对比katalon

作者:互联网

前言:本篇是关于playwright与katalon的录制代码风格比对,前者号称宇宙最强,后者据统计全球第二。关于它们的介绍可自行百度,本篇仅从一个小demo看看二者的风格差异。demo操作:打开百度,输入helloworld,选择百度百科里的结果。

playwright风格

在终端输入python -m playwright codegen,自动打开浏览器之后,执行上面demo中的操作即可。

from playwright.sync_api import sync_playwright

def run(playwright):
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("https://www.baidu.com/")
    page.click("input[name=\"wd\"]")
    page.fill("input[name=\"wd\"]", "helloworld")

with page.expect_navigation(): page.press("input[name=\"wd\"]", "Enter")
with page.expect_navigation(): with page.expect_popup() as popup_info: page.click("text=helloworld - 百度百科") page1 = popup_info.value context.close() browser.close() with sync_playwright() as playwright: run(playwright)

katalon风格

通过谷歌插件katalon recorder,点击record之后,执行上面demo中的操作,最后导出为python格式代码。

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://www.baidu.com/")
        driver.find_element_by_id("kw").clear()
        driver.find_element_by_id("kw").send_keys("helloworld")
        driver.find_element_by_id("form").submit()
        driver.find_element_by_link_text(u"helloworld - 百度百科").click()
        # ERROR: Caught exception [ERROR: Unsupported command [selectWindow | win_ser_1 | ]]

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

分析总结

  可以看到,在执行同样demo的情况下,两个框架的代码风格还是比较大的。playwright无论是代码行数还是语法方面都显得精简很多,是追求优雅的小伙伴的福音;katalon录制的代码里看到了selenium和unittest的影子,技术选型上稍显落后,不过katalon好在什么都可以做,接口自动化、移动端ui自动化都行。

  从接触过的测试框架来看,playwright已是最优解,搭配pytest已经是企业级ui自动化解决方案。(如何搭配?个人的“自动化测试”篇有案例噢);katalon仿佛试图一个框架解决所有的自动化,减少了大量的学习成本,这一点对测试人员很友好,但又仿佛像RF框架一样什么都做不好。

  两个框架共同的缺点很明显,直接录制的代码不适合直接放在项目中,需要基于PO模式进行二次加工,不过这也是所有带录制脚本框架的通病。

  如果是你,你更喜欢哪个框架呢?

标签:playwright,self,driver,alert,katalon,自动化,page
来源: https://www.cnblogs.com/teark/p/14606256.html