A simple look at some of the feelings after Selenium+python

Source: Internet
Author: User

There's always a big difference between open source and charging. Selenium as an open source product, is indeed already very powerful, but for QTP this NB product still appears too small pediatrics. This feeling is like a knowledge testing framework, and the other is the Cloud survey company.

Talk less, coming today is a summary of some of my previous things.

From selenium import Webdriver

Driver = Webdriver. Firefox ()


#访问百度首页

First_url = ' http://www.baidu.com '

Print ("Now access%s"% (Firist_url))

Driver.get (First_url)


#访问新闻界面

Second_url = ' http://news.baidu.com '

Print ("Now access%r"% (Second_url))

Driver.get (Second_url)


#返回首页

Print ("Back to%r"% (First_url))

Driver.back ()


#前进到新闻页面

Print ("forward to%r"% (Second_url))

Driver.forward ()



#返回首页

Print ("Back to%r"% (First_url))

Driver.back ()


#刷新界面

Driver.refresh ()


driver.find_element_by_id ("Idinput"). Clear ()

driver.find_element_by_id ("Idinput"). Send_keys ("Chicken Stew mushroom")

driver.find_element_by_id ("Idinput"). Clear ()

The above is my simple Python-based selenium code, like some of the problems we have encountered in our testing, such as captcha problems, setting assertion issues I'm not talking about. This code is the function of everyone to understand, in which I do not explain.

Selenium the way the page elements are positioned: ID, name, class_name, Tag_name, Link_text, Partial_link_text, XPath, CSS. Positioning method: Webdriver provides the notation: Find_ element_by_id. In this way, we can do some functions of the page through some click, Refresh, Send_keys, and so on. Similar to our manual recording script, to achieve our operation.

We know that in automated testing, if only the script and not the framework is a terrible thing, our script if no framework is worthless, because our use case, our thinking, really rely on the framework to implement, the script is simply a substitute for our test "dot point."

UnitTest provides us with a very good solution, if you learn selenium, we suggest you look at it. UnitTest has four important concepts, namely: Test fixture,test case, Test suite,test runner. Test case is a testing suite that tests a component to organize test cases, test runner is to perform tests, and test fixture builds and destroys the testing case environment. Of course, the above is just the most basic, if we do a more normal automated testing work, we have to use the discover, so as to maximize the use of our script to avoid duplication of writing. People who have had a programming experience should be well understood, which is more like encapsulation calls. Sometimes we can not stop because of an error we run the script, because that is not worth, the cost is very high, all will have some validation and skip.

From selenium import Webdriver

From selenium.webdriver.common.by Import by

From Selenium.webdriver.common.keys import keys

From Selenium.webdriver.support.ui import Select

From selenium.common.exceptions import nosuchelementexception

From selenium.common.exceptions import noalertpresentexception

Import UnitTest

Import time, re


#继承unittest. TestCase

Class Baidutest (UnitTest. TestCase):

#初始化

def setUp (self):

Sslf.driver = Webdriver. Firefox ()

Self.driver.implicitly_wait (30)

Self.base_url = "http://www.baidu.com/"

Self.accept_next_alert = Ture

#测试脚本

def test_baidu (self):

Driver = Self.driver

Driver.get (Self.base_url + "/")

driver.find_element_by_id ("kw"). Clear

driver.find_element_by_id ("kw"). Send_keys ("Flow Ke")

driver.find_element_by_id ("su"). Click ()

#查看定位元素是否存在和异常处理

def is_element_present (self,how,what):

Try

Self.driver.find_element (by = how, value = what)

Except Nosuchelementexception,e:

Return False

Return Ture

#查看是不是有警告框

def is_alert_present (self):

Try

Self.driver.switch_to_alert ()

Except Noalertpresentexception,e:

Return False

Return Ture

#获得警告框内容, close the warning box

def close_alert_and_get_its_text (self):

Try

Alert = Self.driver.switch_to_alert ()

Alert_text = Alert.text

If Self.accept_next_alert:

Alert.accept ()

Else

Alert.dismiss ()

Finally

Self.accept_next_alert = Ture

#结束测试方法

def tearDown (self):

Self.driver.quit ()

Self.assertequal ([],self.verificationerrors)


#通过unittest. Main () Execute test method

if __name__ = = "__mian__":

Unittest.main ()


This article is from the "Test Technical Questions Summary" blog, please be sure to keep this source http://xiaowangu.blog.51cto.com/5707515/1935081

A simple look at some of the feelings after Selenium+python

Related Article

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.