Directory
First, the basic statement
1. Cyclic control (break,continue)
3. Substitution of characters (Replace,repalcefirst,replaceall,regex)
4. Connection of strings ("+", append)
5. Interception and segmentation of strings (Substring,split)
6. Creation and deletion of folders (Mkdirs,delete)
7. Read and write files (bufferedwrite,bufferedreader)
8, System time Acquisition (((Calendar.getinstance ()). Get (Calendar.year))
9. Conversion of time format (SimpleDateFormat)
Second, browser automation operations (based on MAVEN package)
(a) Basic operation of the browser
(ii) Positioning of web elements
1. Name positioning
2. Class positioning
3. CSS Positioning
4. ID Location
5, tagname positioning
6. Link,partiallink Positioning (a)
7. XPath positioning
(iii) Hover action on Mouse (actions)
(iv) Operation of the hover box alert (Accept,dismiss)
(v) switching on the Web frame (frame,defaultcontent)
(vi) page Jump Confirmation (assert,isdisplayed)
Third, the realization of automation function (based on testng)
1. Basic framework
2. Data and Code separation framework (Excel,feedtest)
First, the basic statement
1. Cyclic control (break,continue)
Break is used to end the entire loop;
The Continue is used to end this cycle for the next cycle.
2. Comparison of characters and strings (Compareto,equals,comparetoignorecase,equalsignorecase)
The CompareTo and comparetoignorecase results return an int (the ASCII difference of the first different character of the two string);
The Equals and equalsigonrcase results return a bool value (true, false);
CompareTo and equals are case-sensitive and-ignorecase ignore case.
Example: "abc". COMPARETO ("BAC");
"ABC". EQUALSINGORECASE ("abc");
3. Substitution of characters (Replace,repalcefirst,replaceall,regex)
Sample: "Ab Cdef ab". Replace ("AB", "XY")
Replace: Replaces the AB in the front string with XY, which is case-sensitive;
Repalcefirst: Replace the first AB with XY;
ReplaceAll: Replace all ab with XY;
Strict regular expression: String regex= "^[0-9]{4}$"; indicates that only a number is replaced and only 4 digits in a row;
Non-strict regular expression: String regex= "[0-9]{4}", as long as the existence of a contiguous number of 4-bit digits to replace;
Example: "1324adb568872". Repalce (Regex, "XX"); This expression, if the regex is strict, replaces only 1324, and 1324 and 5688 are replaced if they are non-strict.
4. Connection of strings ("+", append)
"+" can concatenate multiple strings, append is appended to the end of the string, but from a running speed, the append is more efficient and faster than "+".
Example: String str1= "abc" + "BCD";
String str2= "abc". APPEND ("BCD");
5. Interception and segmentation of strings (Substring,split)
Sample: "ABCDEFG". Substring (4);//Result: "EFG", delete the first four characters;
"ABCdef". substring (0,4);//intercept the characters between two positions;
"Abd fghk". Split ("");//split field at Space;?????
6. Creation and deletion of folders (Mkdirs,delete)
String str= "D:/practice1/practice2";
File File=new file (str);
File.mkdirs ();//creation of folders;
File.delete ();//delete;
String str2= "D:/practice1/practice2/11.txt";
File File2=new file (str2);
File2.createnewfile ();//Create a file.
File2.delete ();//delete
7. Read and write files (bufferedwrite,bufferedreader)
BufferedWriter bw=new BufferedWriter (New FileWriter ("filename"));//Create write stream. The creation of the read stream is the same, just swap with BufferedReader ().
Bw.write ("What a sunny day!");
Bw.newline ();//Line break
Bw.close ();//general use of files to close.
Determine if the file exists: String str; (Str=br.readline ())!=null;
8, System time Acquisition (((Calendar.getinstance ()). Get (Calendar.year))
Calendar cal=calender.getinstance ();
int Year=cal.get (calendar.year);//Current year
int Month=cal.get (calendar.month) +1;//system to month statistics is starting from 0, so +1
int Date=cal.get (calendar.date)//day
int Day1=cal.get (Calendar.day_of_week)//day of the week
int Day2=cal.get (Calendar.day_of_month)//first day of January
int Day3=cal.get (calendar.day_of_year)//day of the year
9. Conversion of time format (SimpleDateFormat)
Long Time=system.currenttimemillis ();//Current system date
String str1= "mm/dd/yyyy";
String str2= "YYYY/MM/DD";
String str3= "Yyyy/mm/dd HH:mm:ss";
String str4= "Yyyy/mm/dd HH:mm:SS";
String str5= "Yyyy/mm/dd hh:mm:s";//Time format, H-24-hour, S-second, S-ms
SimpleDateFormat form1=new SimpleDateFormat (str)
System.out.println (Form1.format (time));//Format conversion
Second, browser automation operations (based on MAVEN package)
(a) Basic operation of the browser
System.setproperty ("Webdriver", "c:\\promgramfiles\ Browser physical Location \firefox.exe");//Set default browser.
Webdriver driver=new firefoxdriver ();//Open Browser
Driver.manage (). window (). Maximize ();//maximized
Driver.manage (). Timeouts (). Implicitlywait (5,timeunit.seconds); Smart wait 5 seconds
Thread.Sleep (5000);//wait 5 seconds
Driver.get ("http://url");//Open Web page
Driver.close ();//close Current browser
Driver.quit ();//Close Browser process
(ii) Positioning of web elements
As far as Firefox is concerned, most of the elements can be found with the plugin firebug, only the CSS type needs to be consulted with Seleniunide.
1. Name positioning
Driver.findelement (by.name ("xxx"));
2. Class positioning
Driver.findelement (by.classname ("xxx"));
3. CSS Positioning
Driver.findelement (By.cssselector ("input[type= ' button"));
4. ID Location
Driver.findelement (by.id ("xxx"));
5, tagname positioning
Driver.findelement (by.tagname ("xxx"));
6. Link,partiallink Positioning (a)
Driver.findelenment (By.linktext ("xxx")),//link need to link the entire content behind to locate, and partiallink only need to write a part to be able to find and locate.
7. XPath positioning
DRIVER.FINDELEMEMT (By.xpath ("/html/body/each layer path/target"));
Driver.findelement (By.xpath ("//input[@value = ' value ']");
Driver.findelement (By.xpath ("//img[@alt = ' picture name ']");//The name that is displayed when the picture is named on a Web page, holding down ALT and moving the mouse over the diagram.
Driver.findelement (By.xpath ("//img[contains (@alt, ' xxx ')");
Driver.findelement (By.xpath ("//a[text () = ' xxx ']");//For links through XPath positioning does not need to use @, but in the format given by the example.
(iii) Hover action on Mouse (actions)
Actions action=new actions (driver);//create mouse actions for the browser
Action.movetoelement ("position"). Perform ();//move to a location where perform () is used to confirm.
Action.clickandhold ("position"). Perform ();//Press and move
Action.release ("position"). Perform (); release the mouse
(iv) Operation of the hover box alert (Accept,dismiss)
Driver.switchto (). alert (). SendKeys ("xxx");//go to the hover box and write
Driver.swithcto (). alert (). Accept ();//Confirm
Driver.swithcto (). alert (). dismiss ();//Cancel
(v) switching on the Web frame (frame,defaultcontent)
Driver.switchto (). FRAME ("xxx");
Driver.switchto (). Defaultcontent ();//moving from one frame to another generally requires jumping back to the main frame defautlcontent ()
(vi) page Jump Confirmation (assert,isdisplayed)
Assert.asserttrue (Driver.findelement (By.name ("XX")). isdisplayed ();//confirm the appearance of an element of the page after the jump.
Third, the realization of automation function (based on testng)
1. Basic framework
Package TestNG
public class test{
@Test (dataprovider= "DP")//function of the main implementation structure
public void Mainn (String a,int B,STRINGC) {
}
@DataProvider (name= "DP")//Data Provider
Public object[][] DP () {
Object[][] abc={{"AB", "5", "BC"},{"BC", "3", "XX"}};
}
@Test (dependsonmethods= "Mainn")//Run after Mainn
public void add{
}
}
2. Data and Code separation framework (Excel,feedtest)
Package TestNG;
public class Test extends Feedtest//extends feedtest for Append
{
@Test (dataprovider= "feeder")//feeder as a fixed term
@Souce ("Data/1.xls")//data is a folder path within Eclips, 1.xls is an Excel source file that needs to be copied to eclips for this path to take effect.
public void Mainn (String a,string b,string c,int x) {
}
Summary of Selenium test basics based on Java language