Selenium+python Interview Topic Summary

Source: Internet
Author: User

1. webdriver principle

Webdriver is designed according to Client/server mode, theclient is our test code, sends the request, theserver is the open browser to open the client's request and respond.

Specific work Flow:

· Webdriver opens the browser and binds to the specified port. Start the browser as remote server.

· the client sends HttpRequest to the remote server's listening port via Commandexecuter (thewebdriver Wire Control Communication Protocol)

· Remote server translates to a browser's native call based on the native browser component .

Note: When you start your browser, you are using http:hyper Test Transfer Protocol

2. Selenium features and composition

Selenium is an open-source test framework for Web applications, and its test cases can be developed in HTML table or HTML code or programming languages, and can be executed on almost all current browsers. Selenium can be deployed on the Windows.linux.mac platform, supported languages are Java/python/ruby/.net/perl

Characteristics:

• Supports recording playback

• Position and manipulate page elements in a variety of ways

• Ability to Debug and set breakpoints

• Ability to save test scripts to Html/ruby or other languages

• Support for user extensions in user-extensions.js form

• The ability to make page assertions

1) Selenium IDE: Firefox plugin, can record /replay /test script

2) Selenium RC: Support Program language (Java/c#/python/ruby) to write test cases , Selenium and other test framework integration, such as the Java environment will Selenium and JUnit integration, which uses selenium to write test cases, uses JUnit to automate test cases.

3) Selenium Core: is the core of Selenium, is composed of JS and HTML files, it is Selenium IDE and Selenium RC core engine.

4) Selenium Grid: A Selenium extension that can assign a batch of Selenium scripts to run synchronously on different test machines. That is , the selenium grid can establish a master node (hub) and branch nodes (node) on different hosts , allowing the test cases on the master node to run on different branch nodes. For different nodes, a different test environment (operating system /browser) can be built.

3. Selenium internal operating mechanism

4. How to improve the stability of automation scripts

Find out why

1) Speed reason, increase time wait

2) function reason, use less conflict-prone functions as little as possible

3) Configure testng to implement multi-threading, when writing test cases, be sure to achieve loose coupling, in the case of the server allows to set up multi-threaded operation, improve execution speed.

5. High-quality automation scripting features

Business and code separation, encapsulation type is good.

Automation use case coupling is low, independence is strong, easy to expand maintenance

6. Automated Test defects

Not stable

The reliability is not strong

Difficult to maintain

Costs and benefits

7. Implementation strategy for automated use cases

Based on the purpose of automated execution.

(1) Automated test case execution for monitoring, can set up the use case timing execution;

(2) For regression testing, the test case can be set to trigger execution;

(3) Test cases that do not require frequent execution can be performed manually.

8. Increase the execution speed of selenium scripts

(1) Reduce the operation steps, reduce unnecessary operation;

(2) interrupt page loading, if the page loading content and loading content does not affect our testing, you can set the time-out period, interrupt page loading;

(3) Set the waiting time, you can sleep () fixed time, detect the occurrence of an element after the interruption of waiting can also improve the speed;

(4) Configure testng to implement multi-threading, when writing test cases, it is important to achieve loose coupling, in the case of the server allows to set up multi-threaded operation, improve execution speed.

9. Element positioning

1) ID location

find_element_by_id (' kw ')// location by id attribute

2) name Locator

Find_element_by_name (' wd ')//By the name of the location, the name is generally not unique, generally cannot pass its positioning

3) classname positioning

Find_element_by_class_name (' CN ')//Location by class name

4) tagname positioning

Find_element_by_tag_name (' input ')//tags, generally used for the positioning of a class of elements

5) linktext positioning

Find_element_by_link_text (' News ')//text link locator

6) Partiallinktext positioning

Find_element_by_partial_link_text (' A very long ')//text link part of the text

7) Absolute path positioning

Find_element_by_xpath ('/html/body/div/div[2]/form/span/input[2] ')//absolute address positioning, using hierarchical positioning

Find_element_by_xpath ('//input[@name = ' n ' and id= ' kw '] ')//Current page A directory under name N,the ID of kw C7>input tags, using element attributes for positioning

Find_element_by_xpath ('//*[@id =kw] ')//where * denotes any attribute representing any element of the genus with an ID of kw

Find_element_by_xpath ('//span[@class = ' C ']/input ')//hierarchy and attribute combination for positioning

Find_element_by_xpath ('//a[contains (href, ' test ')]

8) Relative path positioning

Relative path positioning is generally faster than absolute path positioning.

Find_element_by_css_selector ('. Class1 ')//class name positioning . class

Find_element_by_css_selector (' #id ')//id positioning #id

Find_element_by_css_selector (' input ')//Tag name locator

Find_element_by_css_selector (' [Autocomplete=off] ')//attribute positioning

Find_element_by_css_selector (' span>input ')// input child element with tag name span

9) Positioning a set of elements

checkboxes = find_elements_by_tag_name (' checkbox ')

For checkbox in checkboxes:

Checkbox.click () # Select all checkboxes through for loop

Checkboxes.pop (). Click () #取消勾选一些checkbox,Pop ( -1) =pop () represents the last of a set of elements;Pop (0) represents the first,pop (1) of a group of elements Represents the second of a set of elements, and so on

10) Parent -Child/sibling /neighbor-node positioning

• Parent- Child

find_element_by_id (' parent '). Find_element_by_tag_name (' input ') search in tandem

Find_element_by_xpath ('//input/div[@name = ' n ']/span/input[1] ') XPath hierarchy

Find_element_by_css_selector (' div#b>input ') CSS selector parent-child relationship

• parent , child

Find_element_by_xpath ('//div[@id = ' C ']/. /..’) The Father of the father who//div[@id = ' C ')

Brother and brother

Find_element_by_xpath ('//div[@id = ' C '/... /DIV[1]) Find your brother, find your father and find your brother.

Find_element_by_css_selector (' Div#d+div ') represents the Div immediately following the Id=d Div

Find_element_by_css_selector (' Div#d~div ') represents all div at the same level behind the Id=d div

One ) Two-time positioning

Element1 = driver.find_element_by_id (' ID1 ')

Element2 = element1.find_element_by_id (' ID2 ')

The difference between Display:none and hidden

The common denominator: to hide an element in a Web page; they are not located in the selenium.

Difference:Display:none does not preserve its physical space for hidden objects, which disappear completely on this page and cannot be seen /touched;hidden makes an object invisible on a Web page, but the object still occupies space in the Web page and cannot be seen/touched.

Note:iselementpresent () to determine whether an element exists.

10. Common Controls use

1) Link/button:

Element.click ()

Note:Ajax cannot use the clickandwait () function because the cickandwait function waits for the page to reload after the click, and Ajax is a partial refresh, So this page cannot be reloaded to complete.

2) Textbox:

Element.send_keys (' Test ')

3) Upload

DRIVER.FIND_ELEMENT_BY_ID (' id '). Send_keys (' D\test.txt ')

4) Mouse Event:

e1=driver.find_element_by_id (' kw ')

Actionchains (Driver). Double_click ()/.context_click ()/.drag_and_drop ()/.move_to_eleme nt ()/.perform () # double-click /Right-click /drag /hover /Perform behavior in actionchains

5) Dropdown:

· <Select> Tab drop-down menu

From Selenium.webdriver.support.ui import Select

Select (driver.find_element_by_id (' Gender ')). Select_by_index (1)

Select (driver.find_element_by_id (' Gender ')). Select_by_value ("2")

Select (driver.find_element_by_id (' Gender ')). Select_by_visible_text ("Male")

• Non- <Select> tag drop-down menu

Dropdown1 = driver.find_element_by_id (' id ') #先定位到dropdown

dropdown1.find_element_by_id ("li2_input_2") #再定位到value in dropdown

6) Alert:

Driver.switch_to.alert.accept ()//Accept

Driver.switch_to.alert.dismiss ()//Cancel

Message=driver.switch_to.alert.text//Get text messages for pop-up windows

Driver.switch_to.alert.send_keys (' hello ')//input value , this is not available in alert and confirm

7) Window

Driver.refresh () Refresh

Driver.back () Back

Driver.forward () Forward

Driver.maximize_window () Maximize

Driver.set_window_size (100,200) Setting the window size

Driver.switch_to.window (Searchwindow)

8) Frame

Driver.switch_to.frame (Referenceframe)

Driver.switch_to.parent_frame () # frame requires a first-level cut

Driver.switch_to.default_content ()

11. Wait

1) Explicit wait

Wait for a certain condition to continue execution, every time to detect, exceeding the maximum time to throw an exception (the program every x seconds to see, if the condition is set, perform the next step, otherwise wait, exceed the maximum time, throw an exception)

From selenium.webdriver.common.by Import by

From Selelnium.webdirver.support.ui import webdriverwait

From Selenium.webdriver.support import expected_conditions as EC

E = webdriverwait (Driver, 5,0.5). Until (

Ec.presence_of_element_located ((by.id, ' kw '))

)

2) Implicit wait

The time in an implicit wait is not a fixed wait time, and it does not affect the execution speed of the script. For example, when the positioning of an element, if the element can be positioned to continue to execute, if the current position is not located in a poll to continue to determine whether the element is located, if the specified time has not been positioned to throw an exception. (A about B go to dinner, a here waiting, waiting for 10 minutes to arrive, then from 10 minutes after the next step, if waiting 20 minutes, then 20 minutes after the next step, if the maximum time 30 minutes have not yet to throw an exception)

Driver.implicitly_wait (20)

3) Forced wait

From time import sleep

Sleep (2)

12.

Driver.get_screenshot_as_file (' E:\sc.jpg ')

13. Linear Test

High development and maintenance costs

14. Modular Drive Test

Separate the repetitive operations into public modules, eliminating duplication and improving the maintainability of test cases.

15. Data-driven testing

Parameterization of data

1) Read TXT file

file= open (' E:\wtest.txt ', ' R ')

Lines = File.readlines ()

For line in lines:

User = Line.split (', ') [0]

Passw=line.split (', ') [1]

Print (User, PASSW)

2) Read the CSV file

Unicodedecodeerror (no solution found)

3) Read the XML file

16. Keyword-driven testing

Scripting /Data /Business separation

Page Object design mode

is to encapsulate the page object into an HTML page, manipulate the pages elements by providing an application-specific API, rather than searching for objects in HTML, providing an easy-to-program interface and hiding the underlying parts of the window. Encapsulates the interactive details of the interface.

Advantages: Reduced code duplication /improved readability of test cases /improved maintainability of test cases (especially for projects with frequent UI changes)

In the Page object settings mode, you do not need to add an assertion (why) to the method that is located in the page .

Assert Assertion

-The asserted English is assertion, the assertion check of the English is assertion checking.

-An assertion is a logical expression that specifies a state that a program must already exist, or a set of conditions that a program variable must meet at a point during program execution.

Assert determines that if an error occurs, the entire test is interrupted, and the verify is used to determine if an error continues until the end of execution.

Dsfdsadsaf .

DSAF Countdown

Selenium+python Interview Topic Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.