美文网首页
C++使用Selenium WebDriver做UI自动化测试

C++使用Selenium WebDriver做UI自动化测试

作者: FredricZhu | 来源:发表于2021-09-15 15:25 被阅读0次
大部分情况下不需要做UI自动化测试了。因为效率比较低,但是呢,公司有个react和django的自动化smoke,是用yaml文件写的,调试起来比较不方便,所以自己搭建了一下C++的WebDriver Client环境,用来调试各种xpath, css selector等。
  搭建过程。
  参考的这个库,最近还比较活跃,最近的更新在1个月以前。
  https://github.com/GermanAizek/webdriverxx

  但是这个作者的文档写的有点问题,他给的那个jar包不能用,需要下载selenium standalone的jar包,官网上就有。
  https://www.selenium.dev/downloads/

  库下载完以后需要做两点修改,


  第一点是把这个库的头文件放在工程的include的utils目录下。
  https://github.com/GermanAizek/picobase64


  第二点是需要修改一下 include/webdriverxx/browsers/目录下的chrome.h头文件。
struct Chrome : Capabilities { // copyable
    Chrome(const Capabilities& defaults = Capabilities())
        : Capabilities(defaults) {
        SetBrowserName(browser::Chrome);
        SetVersion(defaults.GetVersion());   // 加上这句就可以,BrowserName不能为空
        SetPlatform(platform::Any);
    }

    // See https://sites.google.com/a/chromium.org/chromedriver/capabilities for details
    WEBDRIVERXX_PROPERTIES_BEGIN(Chrome)
    WEBDRIVERXX_PROPERTY(ChromeOptions,             "goog:chromeOptions",                  ChromeOptions)
    WEBDRIVERXX_PROPERTY(PerfLoggingPrefs,          "goog:perfLoggingPrefs",               chrome::PerfLoggingPrefs)
    WEBDRIVERXX_PROPERTIES_END()
};
  加上带红线的那句就可以,因为BrowserName不能为空。

  另外工程运行时需要依赖curl库,可以在CMakeLists.txt文件中链接一下。

CMakeLists.txt文件如下,


cmake_minimum_required(VERSION 2.6)
project(wd_test)

add_definitions(-std=c++17)
add_definitions(-g)

add_definitions(-w)

set(CURL_LIBRARY "-lcurl") 
find_package(CURL REQUIRED)

find_package(Boost REQUIRED COMPONENTS
    system
    filesystem
    serialization
    program_options
    thread
    )

include_directories(${Boost_INCLUDE_DIRS} /usr/local/include /usr/local/iODBC/include /opt/snowflake/snowflakeodbc/include/ ${CMAKE_CURRENT_SOURCE_DIR}/../../ ${CURL_INCLUDE_DIR})

LINK_DIRECTORIES(/usr/local/lib /usr/local/iODBC/lib /opt/snowflake/snowflakeodbc/lib/universal)

file( GLOB APP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
foreach( sourcefile ${APP_SOURCES} )
        file(RELATIVE_PATH filename ${CMAKE_CURRENT_SOURCE_DIR} ${sourcefile})
    
        string(FIND "${filename}"  "test.cpp" "TEMP")
    if( NOT "${TEMP}" STREQUAL "-1" )
        string(REPLACE ".cpp" "" file ${filename})
        add_executable(${file}  ${APP_SOURCES})
        target_link_libraries(${file} ${Boost_LIBRARIES} ${CURL_LIBRARIES})
        target_link_libraries(${file}  ssl crypto libgtest.a libgtest_main.a pystring libgmock.a iodbc iodbcinst libnanodbc.a pthread)
    endif()
endforeach( sourcefile ${APP_SOURCES})

然后启动selenium standalone jar包。运行gtest就可以了。
代码如下,

#include <webdriverxx/webdriverxx.h>

#include <chrono>
#include <thread>
#include <pystring/pystring.h>
#include <gtest/gtest.h>

using namespace webdriverxx;


WebDriver initWebDriver() {
    auto caps = Capabilities();
    caps.SetVersion("93.0.4577.63");
    caps.SetBrowserName(browser::Chrome);
    caps.SetPlatform(platform::Any);
    WebDriver driver = Start(Chrome(caps));
    return std::move(driver);
}

GTEST_TEST(WdTests, TestBaidu) {
    auto driver = initWebDriver();
    driver.Navigate("https://www.baidu.com");
    auto kw = driver.FindElement(ById("kw"));
    kw.SendKeys("Hello");
    auto su = driver.FindElement(ById("su"));
    su.Click();
    std::this_thread::sleep_for(std::chrono::seconds(1));
    auto source = driver.GetPageSource();
    auto source_lower = pystring::lower(source);
    ASSERT_TRUE(source_lower.find("hello") != std::string::npos);
}

程序运行的效果如下,
程序在百度页面点击了一下hello。
然后检查输出页面中有hello。


image.png image.png

相关文章

网友评论

      本文标题:C++使用Selenium WebDriver做UI自动化测试

      本文链接:https://www.haomeiwen.com/subject/juokgltx.html