selenium测试为啥要启动selenium server

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selenium测试为啥要启动selenium server相关的知识,希望对你有一定的参考价值。

Selenium Server主要控制浏览器行为,总的来说,Selenium Server由三部分组成:Launcher、Http Proxy、Selenium Core,其中Selenium Core是在Selenium Server启动浏览器时,嵌入到浏览器中的。Selenium Core是一堆javascript函数集合,当Selenium RC通过网络与 Selenium Server通讯时(通过简单的Http Get或Post请求),Selenium Server解析客户端请求为Selenese Command,然后就会调用相应的JS函数来控制浏览器操作。

(1).测试案例(Testcase)通过Client Lib的接口向Selenium Server发送Http请求,要求和Selenium Server建立连接。

为什么要通过发送Http请求控制Selenium Server而不采用其他方式呢?从上文可以看出,Selenium Server是一个独立的中间服务器(确切地说是代理服务器),它可以架设在其他机器上!所以测试案例通过发送HTTP请求去控制Selenium Server是很正常的。

(2).Selenium Server的Launcher启动浏览器,把Selenium Core加载入浏览器页面当中,并把浏览器的代理设置为Selenium Server的Http Proxy。

(3).测试案例通过Client Lib的接口向Selenium Server发送Http请求,Selenium Server对请求进行解析,然后通过Http Proxy发送JS命令通知Selenium Core执行操作浏览器的动作。

(4).Selenium Core接收到指令后,执行操作。

(5).浏览器收到新的页面请求信息(因为在(4)中,Selenium Core的操作可能引发新的页面请求),于是发送Http请求,请求新的Web页面。

由于Selenium Server在启动浏览器时做了手脚,所以Selenium Server会接收到所有由它启动的浏览器发送的请求。

(6).Selenium Server接收到浏览器的发送的Http请求后,自己重组Http请求,获取对应的Web页面。

(7).Selenium Server的Http Proxy把接收的Web页面返回给浏览器。

因为浏览器存在同源策略,所以Selenium RC中的Selenium Server需要以这种代理模式运行。

参考技术A 为啥要安装呢?直接用不是?
推荐java程序的方式调用,下selenium rc相关的jar,然后,Selenium和Selenium Server的方式直接启动

selenium自动化测试之测试结果验证

1.实际测试过程中,常常要对比实际结果与期望结果是否一致。
2.如果实际结果与期望结果不一致则被认为bug

selenium广泛应用于B/S架构,如何通过selenium来验证测试结果的正确性呢。

  • 案例分析:以百度为例,一起来看看如何验证测试结果的正确性。
    • 点击百度首页的“hao123”后;
    • 跳转至"hao123"页面
    • 验证:是否跳转至“hao123”页面
  • 如何通过selenium实现该场景呢?
#******************
# 获取验证信息,操作某页面后,确认进入的是期望结果
#******************

from selenium import webdriver
from time import sleep

driver = webdriver.Firefox()
driver.get("http://www.baidu.com")

print("===========The baidu Page===============")
First_Title = driver.title
print("The first page title is:%s" % First_Title)

print("===========The hao123 Page===============")
driver.find_element_by_xpath("//a[@name=‘tj_trhao123‘]").click()
Second_Title = driver.title
print("The Second page title is:%s" % Second_Title)

Expect_Title = "hao123_上网从这里开始"
if Second_Title == Expect_Title:
    print(True)
else:
    print(False)
driver.quit()
  • selenium通过driver.title检查页面的title;
  • 判断实际页面的title值是否与期望值相同;
  • 从而得出测试结果。

如果你想系统学习selenium,可以通过如途径哦.....

推广下我的博客专栏,目前选定了一个主题《从零学Selenium自动化测试框架》请添加链接描述,让我们从代码撸起,一步步实现Web自动化测试框架

该专题会从零带你搭建一个可用的自动化测试框架(基于python+selenium)

前提:你要掌握了python与selenium基础哦。要不你看不懂的。

以上是关于selenium测试为啥要启动selenium server的主要内容,如果未能解决你的问题,请参考以下文章

用selenium打开了火狐浏览器但是为啥会闪退

有了selenium为啥还有appium

为啥Selenium rc打开浏览器后就没反应了

为啥我在 Selenium 中收到 AssertionError?

如何启动selenium ide

Selenium实战远程控制JAVA爬虫