JUNIT4 Introduction
JUnit is a simple framework that can write repetitive tests and is an example of a unit test framework based on the Xunit architecture. JUNIT4 is the largest ever improvement in the JUnit framework, and its main goal is to simplify the writing of test cases with the annotation features of JDK 5. JDK 5 can be imported statically, such as import static org.junit.assert.*;. Annotation, also known as annotations (or meta-data), is actually @before, @After, @BeforeClass, @AfterClass, @Test and so on.
The difference between @Before @BeforeClass @After @AfterClass
Simply put, @[email protected] will be executed before and after each method @test annotated, such as there are 5 methods that are @test annotated, then @[email protected] will be executed 5 times. The method that is annotated by @[email protected] generally plays the role of initialization and release. This ensures that each time the @test executes, they are independent of each other and do not interfere with each other.
@[email protected] is executed only once from beginning to the beginning. This approach is recommended for more resource-intensive use.
Of course there are many other differences, see blog:
http://blog.163.com/[email protected]/blog/static/9606199220129682521489/
@Test Execution Order
In general, the methods that are annotated by @test are executed in random order, because unit tests themselves should be independent of each other and can run independently.
However, in some special cases, if you want it to be executed in a certain order, you can use @fixmethodorder (methodsorters.name_ascending), which is placed outside the JUnit test class definition, so that it executes in the ascending manner of the name of the method. At this point, the name of a good method can be easily implemented in a given sequence.
More about JUNIT4
The following blog has more explanation:
http://blog.csdn.net/skyie53101517/article/details/8739126
Http://www.cnblogs.com/caoyuanzhanlang/p/3530267.html
JUNIT4 code block
Put the JUNIT4 test class under the same package as the one under test, so that they are at the same level, so that you do not have to import the tested class.
PackagePkg_selector;Importorg.junit.*;ImportOrg.junit.runners.MethodSorters;ImportOrg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;Import Staticorg.junit.assert.*;/** * Created by Sophie on 15/7/6. * *@FixMethodOrder(methodsorters.name_ascending) Public class selectortest { Private StaticSelector s;Private StaticWebdriver Afterlogin;Private StaticWebdriver Afterswitch;Private StaticWebdriver Aftermousemove;Private StaticWebdriver Aftersearch;Private StaticWebdriver AfterSelect;@BeforeClass Public Static void setUp()throwsException {s =NewSelector (); }@AfterClass Public Static void TearDown()throwsException {s.teardown (); }@Test Public void Test1_login()throwsException {afterlogin = S.login (); Assertequals ("Mining wealth community, the largest financial community in China", Afterlogin.gettitle ()); }@Test Public void Test2_switchwindow()throwsException {afterswitch = S.switchwindow (); Assertequals ("Financial planning, financial management School-mining community, the largest financial community in the country, money-loving people are here.", Afterswitch.gettitle ()); }@Test Public void Test3_mousemove()throwsException {aftermousemove = S.mousemove (); Assertequals ("User", Aftermousemove.findelement (By.cssselector ("A.SHOWMENU.XG1"). GetText ()); }@Test Public void Test4_search()throwsException {aftersearch = S.search (); Assertequals ("Find friends-dig the wealth community", Aftersearch.gettitle ()); }@Test Public void Test5_select()throwsinterruptedexception {afterselect = S.select (); Assertequals (true, Afterselect.getpagesource (). Contains ("The following is the list of users found (up to 100)")); }}
JUNIT4 Running Results
All 5 test passes are:
Code block under test
PackagePkg_selector;ImportOrg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;ImportOrg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.firefox.FirefoxDriver;ImportOrg.openqa.selenium.interactions.Actions;ImportOrg.openqa.selenium.support.ui.Select;ImportJava.util.Iterator;ImportJava.util.List;ImportJava.util.Scanner;ImportJava.util.Set;ImportJava.util.concurrent.TimeUnit;/** * Created by Sophie on 15/7/6. * * Public class Selector { PrivateWebdriver Dr;PrivateString URL;PrivateString account;PrivateString pwd; Public Selector() { This. url ="https://www.wacai.com/user/user.action?url=http%3A%2F%2Fbbs.wacai.com%2Fportal.php";//scanner input = new Scanner (system.in); //system.out.println ("Please input the account"); This. Account ="******";//input.next (); //system.out.println ("Please input the password"); This. PWD ="******";//input.next (); This. Dr =NewFirefoxdriver (); This. Dr.manage (). Timeouts (). Implicitlywait ( -, timeunit.seconds); } PublicWebdriverLogin( )throwsException {//LoginDr.get (URL); Dr.findelement (By.id ("Account"). Clear (); Dr.findelement (By.id ("Account"). SendKeys (account); Dr.findelement (By.id ("PWD"). Clear (); Dr.findelement (By.id ("PWD"). SendKeys (PWD); Dr.findelement (By.id ("LOGIN-BTN"). Click ();returnDr } PublicWebdriverSwitchwindow()throwsException {//Open Financial Planning sub-sectionString a = Dr.findelement (By.linktext ("Financial Planning"). getattribute ("href");//Open the page directly with Webdriver.get (), the browser will not open a new page, save the switch windowDr.get (a); Dr.manage (). window (). Maximize ();//Then close left navigation barDr.findelement (By.cssselector ("A.comiis_left_closes"). Click ();returnDr } PublicWebdriverMouseMove()throwsException {//Click to open the Search Type drop-down box, move the mouse over the user and selectActions act =NewActions (DR); Webelement DropDown = dr.findelement (By.cssselector ("A.SHOWMENU.XG1")); Webelement user = Dr.findelement (by.cssselector ("ul#comiis_twtsc_type_menu>li>a[rel= ' user ']")); Act.click (DropDown). Perform (); Act.movetoelement (user). Click (). Perform (); Act.movebyoffset ( -, -). Click (). Perform ();returnDr } PublicWebdriverSearch()throwsException {//Find the Search input box and enter the keyword, then click the Search buttonWebelement input = dr.findelement (By.id ("Comiis_twtsc_txt")); Input.clear (); Input.sendkeys ("Jay Chou"); Dr.findelement (By.id ("COMIIS_TWTSC_BTN"). Click ();//click Firefox opens a new page after the //Use windowhandle+ page title to switch Dr to the window we wantString Currentwindow = Dr.getwindowhandle (); Set<string> handles = Dr.getwindowhandles ();//system.out.println (Handles.size ());Iterator h = handles.iterator (); while(H.hasnext ()) {dr = Dr.switchto (). window ((String) H.next ());//system.out.println (Dr.gettitle ()); if(Dr.gettitle (). Equals ("Find friends-dig the wealth community")) Break; }returnDr } PublicWebdriverSelect()throwsinterruptedexception {dr.findelement (By.cssselector ("html>body#nv_home.pg_spacecp.sour>div#wp.wp.cl>div#ct.ct2_a.wp.cl>div.mn>div.bm.bw0> Ul.tb.cl>li:nth-child (2) >a "). Click ();/ * Handle the drop-down box dr.findelement (By.xpath ("//select[@id = ' resideprovince ']/option[@value = ' Zhejiang province ') with the normal positioning +click mode. Click ( ); Thread.Sleep (2000); Dr.findelement (By.xpath ("//select[@id = ' residecity ']/option[@value = ' hangzhou city ')"). Click (); */ //Use Select ModeSelect sprovince =NewSelect (Dr.findelement (By.xpath ("//select[@id = ' resideprovince ']"))); Sprovince.selectbyvalue ("Zhejiang province"); Thread.Sleep ( -); Select scity =NewSelect (Dr.findelement (By.xpath ("//select[@id = ' residecity ']"))); Scity.selectbyvalue ("Hangzhou City"); Dr.findelement (By.cssselector ("BUTTON.PN"). Click ();returnDr } Public void TearDown()throwsException {dr.quit (); } Public Static void Main(string[] args)throwsException {Selector s =NewSelector (); S.login (); S.switchwindow (); S.mousemove (); S.search (); Webdriver selectpage = S.select ();//xpath Fuzzy LookupList<webelement> SearchResult = selectpage.findelements (By.cssselector ("li.bbda.cl")); System.out.println ("Search to"+ searchresult.size () +"A user of the same city, they are:"); System.out.printf ("%-20s%-20s%-20s\n","User name","User Level","User points"); String UserName; String UserLevel; String Usermark =""; String[] Sarray; for(inti =0; I < searchresult.size (); i++) {Sarray = Searchresult.get (i). Findelement (By.cssselector ("Li>p"). GetText (). Split (" ");if(Sarray.length <2) Usermark ="0";ElseUsermark = sarray[3]; UserLevel = sarray[0]; UserName = Searchresult.get (i). Findelement (By.cssselector ("H4>a"). GetText (); System.out.printf ("%-20s%-20s%-20s\n", Username,userlevel,usermark); S.teardown (); } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Write unit test code with JUNIT4