今天有同學問到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.什么是持續集成?
可以自行百度,學習能力自我提升很重要(1079636098)軟體測驗技術交流群推薦,
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.selenium 是否可以呼叫js來對dom物件進行操作?
Could selenium call js for implementation dom object directly?
是
29.selenium 是否可以向頁面發送滑鼠滾輪操作?
Could selenium send the action of mouse scroll wheel?
不能
30selenium 是否可以模擬拖拽操作?
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
最后:
歡迎關注公眾號:程式員一凡,領取一份216頁pdf檔案的Python自動化測驗工程師核心知識點總結!
這些資料的內容都是面試時面試官必問的知識點,篇章包括了很多知識點,其中包括了有基礎知識、Linux必備、Shell、互聯網程式原理、Mysql資料庫、抓包工具專題、介面測驗工具、測驗進階-Python編程、Web自動化測驗、APP自動化測驗、介面自動化測驗、測驗高級持續集成、測驗架構開發測驗框架、性能測驗、安全測驗等,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/466.html
標籤:其他
