32道 Selenium 软件测试面试问题

Time will tell.

今天有同学问到面试 Seleinum 的时候会问到哪些问题,于是想了想,暂时纪录下来了这么多,然后欢迎大家在评论区提供更多问题。

1、selenium 中如何判断元素是否存在?

selenium中没有提供原生的方法判断元素是否存在,一般我们可以通过定位元素+异常捕获的方式判断。

# 判断元素是否存在try:

    dr.find_element_by_id('none')except NoSuchElementException:

    print 'element does not exist'

2、selenium 中 hidden 或者是 display = none 的元素是否可以定位到?

不可以,selenium 不能定位不可见的元素。display=none 的元素实际上是不可见元素。

3、selenium 中如何保证操作元素的成功率?也就是说如何保证我点击的元素一定是可以点击的?

被点击的元素一定要占一定的空间,因为 selenium 默认会去点这个元素的中心点,不占空间的元素算不出来中心点;

被点击的元素不能被其他元素遮挡;

被点击的元素不能在viewport之外,也就是说如果元素必须是可见的或者通过滚动条操作使得元素可见;

使用element.is_enabled()(python代码)判断元素是否是可以被点击的,如果返回false证明元素可能灰化了,这时候就不能点。

4、如何提高 selenium 脚本的执行速度?

使用效率更高的语言,比如 java 执行速度就快过 python ;

不要盲目的加 sleep,尽量使用显示等待;

对于firefox,考虑使用测试专用的profile,因为每次启动浏览器的时候firefox会创建1个新的profile,对于这个新的profile,所有的静态资源都是从服务器直接下载,而不是从缓存里加载,这就导致网络不好的时候用例运行速度特别慢的问题;

chrome浏览器和safari浏览器的执行速度看上去是最快的;

可以考虑分布式执行或者使用selenium grid。

5、用例在运行过程中经常会出现不稳定的情况,就是说这次可以通过,下次就没办法通过,如何去提升用例的稳定性?

测试专属profile,尽量让静态资源缓存;

尽量使用显示等待;

尽量使用测试专用环境,避免其他类型的测试同时进行,对数据造成干扰。

6、你的自动化用例的执行策略是什么?

每日执行:比如每天晚上在主干执行一次;

周期执行:每隔2小时在开发分之执行一次;

动态执行:每次代码有提交就执行。

7、什么是持续集成?

可以自行百度,学习能力自我提升很重要(175317069)软件测试技术交流群推荐。

8、自动化测试的时候是不是需要连接数据库做数据校验?

一般不需要,因为这是单元测试层做的事情,在自动化测试层尽量不要为单元测试层没做的工作还债。

9、id,name,clas,x path, css selector这些属性,你最偏爱哪一种,为什么?

xpath和css最为灵活,所以其他的答案都不够完美。

10、如何去定位页面上动态加载的元素?

显示等待。

11、如何去定位属性动态变化的元素?

找出属性动态变化的规律,然后根据上下文生成动态属性。

12、点击链接以后,selenium是否会自动等待该页面加载完毕?

java binding在点击链接后会自动等待页面加载完毕。

13、webdriver client的原理是什么?

selenium的原理涉及到3个部分,分别是:

浏览器

driver: 一般我们都会下载driver

client: 也就是我们写的代码

client其实并不知道浏览器是怎么工作的,但是driver知道,在selenium启动以后,driver其实充当了服务器的角色,跟client和浏览器通信,client根据webdriver协议发送请求给driver,driver解析请求,并在浏览器上执行相应的操作,并把执行结果返回给client。这就是selenium工作的大致原理。

14、webdriver的协议是什么?

client与driver之间的约定,无论client是使用java实现还是c#实现,只要通过这个约定,client就可以准确的告诉drier它要做什么以及怎么做。

webdriver协议本身是http协议,数据传输使用json。

这里有webdriver协议的所有endpoint,稍微看一眼就知道这些endpoints涵盖了selenium的所有功能。

15、启动浏览器的时候用到的是哪个webdriver协议?

New Session,如果创建成功,返回sessionId和capabilities。

16、什么是page object设计模式?

简单来说就是用class去表示被测页面。在class中定义页面上的元素和一些该页面上专属的方法。

例子:


public class LoginPage { private final WebDriver driver; public LoginPage(WebDriver driver) { this.driver = driver; // Check that we're on the right page. if (!"Login".equals(driver.getTitle())) { // Alternatively, we could navigate to the login page, perhaps logging out first throw new IllegalStateException("This is not the login page"); } } // The login page contains several HTML elements that will be represented as WebElements. // The locators for these elements should only be defined once. By usernameLocator = By.id("username"); By passwordLocator = By.id("passwd"); By loginButtonLocator = By.id("login"); // The login page allows the user to type their username into the username field public LoginPage typeUsername(String username) { // This is the only place that "knows" how to enter a username driver.findElement(usernameLocator).sendKeys(username); // Return the current page object as this action doesn't navigate to a page represented by another PageObject return this; } // The login page allows the user to type their password into the password field public LoginPage typePassword(String password) { // This is the only place that "knows" how to enter a password driver.findElement(passwordLocator).sendKeys(password); // Return the current page object as this action doesn't navigate to a page represented by another PageObject return this; } // The login page allows the user to submit the login form public HomePage submitLogin() { // This is the only place that submits the login form and expects the destination to be the home page. // A seperate method should be created for the instance of clicking login whilst expecting a login failure. driver.findElement(loginButtonLocator).submit(); // Return a new page object representing the destination. Should the login page ever // go somewhere else (for example, a legal disclaimer) then changing the method signature // for this method will mean that all tests that rely on this behaviour won't compile. return new HomePage(driver); } // The login page allows the user to submit the login form knowing that an invalid username and / or password were entered public LoginPage submitLoginExpectingFailure() { // This is the only place that submits the login form and expects the destination to be the login page due to login failure. driver.findElement(loginButtonLocator).submit(); // Return a new page object representing the destination. Should the user ever be navigated to the home page after submiting a login with credentials // expected to fail login, the script will fail when it attempts to instantiate the LoginPage PageObject. return new LoginPage(driver); } // Conceptually, the login page offers the user the service of being able to "log into" // the application using a user name and password. public HomePage loginAs(String username, String password) { // The PageObject methods that enter username, password & submit login have already defined and should not be repeated here. typeUsername(username); typePassword(password); return submitLogin(); } }

17、什么是page factory设计模式?

实际上是官方给出的java page object的工厂模式实现。

18、怎样去选择一个下拉框中的value=xx的option?

使用select类,具体可以加群了解

19、如何在定位元素后高亮元素(以调试为目的)?

使用javascript将元素的border或者背景改成黄色就可以了。

20、什么是断言?

可以简单理解为检查点,就是预期和实际的比较

如果预期等于实际,断言通过,测试报告上记录pass

如果预期不等于实际,断言失败,测试报告上记录fail

21、如果你进行自动化测试方案的选型,你会选择哪种语言,java,js,python还是ruby?

哪个熟悉用哪个

如果都不会,团队用哪种语言就用那种

22、page object设置模式中,是否需要在page里定位的方法中加上断言?

一般不要,除非是要判断页面是否正确加载。

Generally don’t make assertions

23、page object设计模式中,如何实现页面的跳转?

返回另一个页面的实例可以代表页面跳转。


// The login page allows the user to submit the login form public HomePage submitLogin() { // This is the only place that submits the login form and expects the destination to be the home page. // A seperate method should be created for the instance of clicking login whilst expecting a login failure. driver.findElement(loginButtonLocator).submit(); // Return a new page object representing the destination. Should the login page ever // go somewhere else (for example, a legal disclaimer) then changing the method signature // for this method will mean that all tests that rely on this behaviour won't compile. return new HomePage(driver); }

24、自动化测试用例从哪里来?

手工用例的子集,尽量

简单而且需要反复回归

稳定,也就是不要经常变来变去

核心,优先覆盖核心功能

25、你觉得自动化测试最大的缺陷是什么?

实现成本高

运行速度较慢

需要一定的代码能力才能及时维护

26、什么是分层测试?

画给他/她看。

27、webdriver可以用来做接口测试吗?

不用纠结,不可以。

28、elenium 是否可以调用js来对dom对象进行操作?

Could selenium call js for implementation dom object directly?

是。

29、selenium 是否可以向页面发送鼠标滚轮操作?

Could selenium send the action of mouse scroll wheel?

不能。

30、selenium 是否可以模拟拖拽操作?

Does selenium support drag and drop action?

可以。

31、selenium 对下拉列表的中的选项进行选择操作时,需要被操作对象的标签是什么?

When Selenium selects the option in selenium, What tag the DOM object should be?

select。

32、selenium 上传文件操作,需要被操作对象的type属性是什么?

When Selenium upload a file, what value of type of the DOM object should be?

file。


对Python自动化软件测试感兴趣可以加入我们扣裙一起学习175317069。有测试学习资源,行业技术人分析讲解。

看个签,有不定期自动化软件测试书籍抽奖福利。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容