Java selenium notes

Directory 1. Basic statements 1. Break, continue 3. Replace, replacefirst, replaceall, regex 4. Connect strings ("+", append) 5. Intercept and split strings 6. Create and delete folders (mkdirs, delete) 7. Read and write files (bufferedwrite, BufferedReader) 8. System time acquisition (((calendar. Getinstance()). Get (calendar. Year)) 9. Time format conversion (simpledateformat) 2. Browser automation operation (based on Maven package) (I) basic operation on browser (II) Page element positioning 1. Name positioning 2. Class positioning 3. CSS positioning 4. ID positioning 5. TagName positioning 6. Link, partialink positioning (a) 7. XPath positioning (III) mouse hover actions (IV) accept, disassiss (V) switch on the page frame (frame, defaultcontent) (VI) Web page Jump confirmation (assert, isdisplayed) III. realization of automation function (based on TestNG) 1. Basic framework 2. Framework for separating data and coding (Excel, feedtest) 1. Break, continue is used to end the whole cycle; continue is used to end this cycle for the next cycle. 2. Comparison of characters and strings (CompareTo, equals, comparetoignorecase, equalsignorecase) CompareTo and comparetoignorecase results return int (ASCII difference of the first different character of two strings); equals and equalsignorecase results return bool values (true, false); CompareTo and equals are case sensitive, and - ignorecase ignores case. Examples: "ABC". CompareTo ("bac"); "ABC" . equalsingorecase ("ABC"); 3. Character replacement (replace, sans serif '> like: "ab CDEF AB". Replace ("ab", "XY") replace: replace AB in the preceding string with XY, case sensitive; replacefirst: replace the first ab with XY; replaceall: replace all AB with XY; strict regular expression: String regex = "^ [0-9] {4} $" ; indicates that only 4 consecutive digits are replaced; non strict regular expression: String regex = "[0-9] {4}", replace as long as there are 4 consecutive digits; for example: "1324adb568872". Reply (regex, "XX"); / / if the regex is strict, only 1324 will be replaced; if it is non strict, 1324 and 5688.4, string connection ("+", append) "+" will be replaced Multiple strings can be connected. Append is appended to the end of the string. However, in terms of running speed, append is more efficient and faster than "+". For example: string STR1 = "ABC" + "BCD"; string STR2 = "ABC". Append ("BCD"); 5. String interception and segmentation (substring, sans serif '> like: "ABCDEFG". Substring (4); / / result: "EFG", the first four characters are deleted; "ABCDEF" . substring (0,4); / / intercept the character between two positions; "abd fghk". Split (""); / / split the field in the space;????? 6. Create and delete folder (mkdirs, sans serif '> string STR = "D: / practice1 / practice2"; file = new file (STR); file. Mkdirs(); / / create folder; file. Delete(); / / delete; string STR2 = "D: / practice1 / practice2 / 11. TXT" ; file File2 = new file (STR2); File2. Createnewfile(); / / create a file. File2. Delete(); / / delete 7. File read / write (bufferedwrite, sans serif '> bufferedwriter BW = new bufferedwriter (New filewriter ("filename")); / / create a write stream. The same is true for the creation of a read stream. Just use bufferedreader(). Bw.write ("what a sunny day!"); BW. Newline(); / / line feed BW. Close(); / / usually close the file when it is used up. Judge whether the file exists: String str; (STR = br. Readline())! = null; 8. Obtain the system time (((calendar. Getinstance()). Get (calendar. Year)) calendar cal = calendar. Getinstance(); int year = cal. Get (calendar. Year); / / current year int month = cal. Get (calendar. Month) +1. / / the system starts from 0, so it needs to + 1int date = cal.get (calendar. Date) / / day int Day1 = cal.get (calendar. Day_of_week) / / the day of the week int Day2 = cal.get (calendar. Day_of_month) / / the day of the month int day3 = cal.get (calendar. Day_of_year) / / the day of the year 9. Time format conversion (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 system, s-seconds, s-milliseconds simpledateformat format format 1 = new simpledateformat (STR) system.out.println (form1. Format (time)); / / format conversion 2. Browser automation (based on Maven package) (1) basic browser operations system.setproperty ("webdriver", "C: \ \ promgrammfiles \ browser physical location \ Firefox. Exe"); / / set the default browser. Webdriver driver = new Firefox driver(); / / open browser driver. Manage(). Window(). Maximize(); / / maximize driver. Manage() . timeouts(). Implicitywait (5, timeunit. Seconds); intelligently wait for 5 seconds thread.sleep (5000); / / wait for 5 seconds driver.get ("http: / / Web address"); / / open the web page driver. Close(); / / close the current browser driver. Quit(); / / close the browser process (2) Location of web page elements as far as Firefox browser is concerned, most elements can be found with the plug-in firebug, and only CSS types need to be viewed with selenium ide. 1. Name locate driver.findelement (by. Name ("XXX"); 2. Class locate driver.findelement (by. Classname ("XXX")); 3. CSS locate driver.findelement (by. Cssselector ("input [type ='button ']) ; 4. ID locating driver.findelement (by. ID ("XXX"); 5. TagName locating driver.findelement (by. TagName ("XXX"); 6. Link, sans serif '> driver.findelement (by. Linktext ("XXX"); / / link needs all the contents behind the link to locate, while partialink only needs to write part to locate. 7. XPath locating driver.findelement (by. XPath)(“ /HTML / body / path / target of each layer "); driver.findelement (by. XPath (" / / input [@ value = 'value'] "); driver.findelement (by. XPath (" / / img [@ ALT = 'picture name'] "); / / the picture name is the name displayed when you press and hold ALT on the web page and move the mouse over the figure. Driver.findelement (by. XPath (" / / img [contains (@ alt, 'xxx') "); driver.findelement (by. XPath (" / / a [text() ='xxx '] ") ; / / you don't need to use @, but use the format given in the example to locate links through XPath. (3) hover over the mouse (actions) actions = new actions (driver); / / create a mouse action for the browser. Action. Movetoelement ("location"). Perform(); / / move to a location, and perform() is used for confirmation. Action. Clickandhold ("location"). Perform() ; / / press and hold action. Release ("position"). Perform(); release the mouse driver. Switchto(). Alert(). Sendkeys ("XXX"); / / go to the hover box and write driver. Switchto(). Alert(). Accept(); / / confirm driver. Switchto(). Alert(). Deny(); / / cancel driver. Switchto(). Frame ("XXX"); driver. Switchto(). Defaultcontent() ; / / when moving from one frame to another, you generally need to jump back to the main frame defautlcontent() assert.asserttrue (driver. Findelement (by. Name ("XX")). Isdisplayed(); / / confirm the appearance of an element on the page after the jump. 1. The basic framework package testngpublic class test {@ test (DataProvider = "DP") / / the main implementation structure of the function public void main (string a, int b, stringc) {} @ DataProvider (name = "DP") / / data provider public object [] [] dp() {object [] [] ABC = {{"ab", "5", "BC"}, {"BC", "3", "XX"}}; return ABC; @ test (dependsonmethods = "mainn") / / after mainn, run public void add {} 2, data and code separation framework (Excel, feedtest) Package TestNG; public class test extends feedtest / / extends feedtest is the addition of {@ test (DataProvider = "feeder") / / feeder is the fixed term @ souce ("data / 1. XLS") / / data is a folder path in ECLIPS, and 1.xls is the Excel source file. It needs to be copied to ECLIPS to take effect.}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>