This article explains the use of selenium by code, while saving the code.
1. Create the test report file, where the XML file is used
public class Filecreate {/** * @param args * Create heml file/public void createhtml (String file_name) {
File File = new file (file_name);
System.out.println ("Create File Method");
if (!file.exists ()) {try {file.createnewfile ();
The file is create SYSTEM.OUT.PRINTLN ("Create file OK");
@SuppressWarnings ("resource") FileOutputStream out=new FileOutputStream (file,true);
StringBuffer sb=new StringBuffer ();
Sb.append ("
2, by adding the relevant log in the test report, to display the test steps, test results.
public class FileLog {/** * * * @param file_name: Files that need to be added log, using HTML file * @param title: facility Title * @param flag * @throws IOException */public void Addlogtitle (String file_name,string Title,boolean flag) throws ioexception{F
ile file = new file (file_name);
@SuppressWarnings ("resource") FileOutputStream out=new FileOutputStream (file,true);
StringBuffer sb=new StringBuffer (); if (flag = = True) {sb.append ("<div><a onclick=\" return Click_b (\ "+ title +" \) \ style=\ Cursor:pointer
; color: #FF0000 \ ">" + title + "</a>"); }else {sb.append ("<div><a onclick=\" return Click_b (\ "+ title +" \ ") \ style=\ Cursor:pointer;color: #00
8000\ ">" + title + "</a>");
} out.write (Sb.tostring (). GetBytes ("Utf-8")); public static void Replacetxtbystr (String file_name,string oldstr,string replacestr) {String Temp
= ""; try {File file = new file (file_name);
FileInputStream fis = new FileInputStream (file);
InputStreamReader ISR = new InputStreamReader (FIS);
BufferedReader br = new BufferedReader (ISR);
StringBuffer buf = new StringBuffer (); Save the contents of the previous line for (int j = 1; (temp = Br.readline ())!= null &&!temp.equals (OLDSTR);
J + +) {buf = Buf.append (temp);
BUF = Buf.append (System.getproperty ("Line.separator"));
//insert content into BUF = Buf.append (REPLACESTR); Save the content following the line while (temp = Br.readline ())!= null) {buf = Buf.append (System.getproperty ("Lin
E.separator "));
BUF = Buf.append (temp);
} br.close ();
FileOutputStream fos = new FileOutputStream (file);
PrintWriter pw = new PrintWriter (FOS);
Pw.write (Buf.tostring (). ToCharArray ()); Pw.flush ();
Pw.close ();
catch (IOException e) {e.printstacktrace (); } public void AddLogTitle11 (String file_name,string title) throws ioexception{File File = new file (file_
name);
@SuppressWarnings ("resource") FileOutputStream out=new FileOutputStream (file,true);
String sb = title;
Out.write (Sb.tostring (). GetBytes ("Utf-8"));
public void Addlogstep (string file_name,string title, string flag, String log) throws ioexception{
File File = new file (file_name);
@SuppressWarnings ("resource") FileOutputStream out=new FileOutputStream (file,true);
StringBuffer sb=new StringBuffer ();
String time = GetTime (); Sb.append ("<div><a onclick=\" return click_a (\ "+ title +" \ ") \ style=\" Cursor:pointer;color: #FF0000 \ "
> "+ title +" </a> "); Add Log sb.append ("<div name=\" "+ title +" \"Style=\" Display:none;color: #008000 \ ">" + log + "</div>");
Sb.append ("<br/>"); Add image if (flag!= null) {sb.append ("
3, after the streamlining of the add test results log code
public class Filelognew {public static void main (string[] args) throws IOException {//TODO auto-generated Method St
UB//stringbuffer SB = new StringBuffer ();
String filename = "./test.html";
Addlogtitle (SB, "Test 002");
Addlogstep (SB, "", "Test 002", "Step 1");
ReplaceText (SB, "FF0000", "008000");
Addlog (filename, SB);
SYSTEM.OUT.PRINTLN ("Test is finished."); /** * * @param sb:stringbuffer temporary variable * @param title: Test Case title, and as control label for this test example * @return: return stringbuffer temporary variable * @thro WS IOException */public StringBuffer addlogtitle (StringBuffer sb, String title) throws IOException {Sb.append ("&
Lt;div><a onclick=\ "return Click_b (\" + title + "\") \ "Style=\" Cursor:pointer;color: #FF0000 \ ">" + title
+ "</a>");
System.out.println ("add title");
return SB; /** * * @param sb:stringbuffer temporary variable * @param imagename: If the variable has a value, the step includes a screenshot, otherwise set to "" @param title: The test case title, and as the Control label for test example * @param log: Added test steps * @reTurn:stringbuffer Temporary variable * @throws ioexception * * Public stringbuffer addlogstep (StringBuffer sb, String imagename, STR ing title, String log) throws IOException {//add log sb.append ("<div name=\" "+ title +" \ "
Style=\ "Display:none;color: #008000 \ >" + log + "</div>"); Add image if (imagename!= "") {sb.append (" ");
return SB;
/** * * @param sb:stringbuffer temporary variable * @param older: replaced old text * @param newer: replaced new text * @return: StringBuffer temporary variable * @throws IOException */Public StringBuffer replacetext (StringBuffer sb, string older, string newer) throws Ioexcept
ion {int index= sb.lastindexof (older);
Sb.replace (index, index+7, newer);
return SB;
/** * Reverse Lookup string FF0000, then replace with 008000 * @param sb:stringbuffer Temporary variable * @return: StringBuffer temporary variable * @throws IOException * Public StriNgbuffer Changelastredtogreen (StringBuffer sb) throws IOException {int index= sb.lastindexof ("FF0000");
Sb.replace (Index, index+6, "008000");
return SB;
/** * Reverse Lookup string 008000, then replace with FF0000 * @param sb:stringbuffer Temporary variable * @return: StringBuffer temporary variable * @throws IOException * * Public StringBuffer changelastgreentored (StringBuffer sb) throws IOException {int index= sb.lastindexof ("008000
");
Sb.replace (Index, index+6, "FF0000");
return SB; /** * Create the file, then add stringbuffer temporary variable to the file * @param file_name: The name of the file created, if it does not exist then create * @param buffer:stringbuffer temporary variable * @throws IOException */public void Addlog (String file_name, StringBuffer buffer) throws IOException {File File = new
File (file_name);
@SuppressWarnings ("resource") fileoutputstream out = new FileOutputStream (file, true);
String sb = Buffer.tostring ();
Out.write (Sb.getbytes ("Utf-8")); /** * * @return: Acquired system time */Public String GetTime () {Long Time=system.currenttimemillis ();
Long now = Android.os.SystemClock.uptimeMillis ();
SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Date date_new = new Date (time);
String time_new = Format.format (date_new);
return time_new;
}
}
4. Create a Test folder
public class FolderCreate {
/**
* Create folders
* @param folder
/public void CreateFolder (String folder) {
System.out.println ("Create Folder Method");
File Dirfirstfolder = new file (folder);
if (!dirfirstfolder.exists ())
{
System.out.println ("Create folder OK");
Dirfirstfolder.mkdirs ();
System.out.println ("Create folder OK");}}
5, Common method
public class Common {
/**
* screenshot feature
* @param webdriver
* @param directory: Screenshot path
* @param Capturename: Screenshot name
* @param format: screenshot/public
static void Capturescreen (Webdriver webdriver, String Directory,string capturename, String format) {
File screenshotfile = ((takesscreenshot) webdriver). Getscreenshotas (outputtype.file);
try {
fileutils.copyfile (screenshotfile, new File (Directory + "/" + Capturename +. "+ format));
} catch (Ioex Ception e) {
e.printstacktrace ();
}
}
/**
* To determine whether a lookup control exists
* @param webdriver
* @param Locator: How to find Controls
* @return/
Public Static Boolean elementexist (Webdriver Webdriver, by Locator) {
try {
webdriver.findelement (Locator);
return true;
} catch (Org.openqa.selenium.NoSuchElementException ex) {return
false;}}}
6, the specific code realization
public class QA {public static string folder = "./testdata";//The folder that is created for the test to hold the test data public static string Folderimage
= "./testdata/images"; private static String file = "result.html";
The HTML file created for the test here is private static String flag = "";
private static FolderCreate Folderc = new FolderCreate ();
private static Filecreate Filec = new Filecreate ();
private static Filelognew Filel = new Filelognew ();
private static Webdriver Firfox = new Firefoxdriver ();
public static void Main (string[] args) throws Exception {Folderc.createfolder (folder);
Folderc.createfolder (Folderimage);
Filec.createhtml (folder + "/" + file);
A ();
Second (); public static void A () throws Interruptedexception, IOException {String title = "a" + "_" + filel.gettime ()
;
StringBuffer sb = new StringBuffer ();
String imagename = "";
System.out.println ("Step 1:open" + "Web_name" + "app");
Filel.addlogtitle (SB, title); Firfox.get ("Http://www.baidu. com ");
Thread.Sleep (2000);
imagename = "Step 1";
Filel.addlogstep (SB, imagename, title, "Step 1:web is opened.");
Webelement input = firfox.findelement (By.name ("WD"));
Input.sendkeys ("abc");
Thread.Sleep (2000);
imagename = "Step 2";
Filel.addlogstep (SB, imagename, title, "Step 2:input keyword.");
Webelement submit = Firfox.findelement (By.id ("su"));
if (submit.isdisplayed ()) {Submit.click ();
Thread.Sleep (2000);
} imagename = "Step 3";
Filel.addlogstep (SB, imagename, title, "Step 3:click submit button.");
if (Common.elementexist (Firfox, By.classname ("t555555555")) {imagename = "Step 4";
Filel.addlogstep (SB, imagename, title, "Step 4:new page is opened");
Filel.changelastredtogreen (SB);
Filel.addlog (folder + "/" + file, SB);
Common.capturescreen (Firfox, Folderimage, imagename, "PNG");
System.out.println ("Step 4:new page is opened");
}else{imagename = "Step 4"; FilEl.addlogstep (SB, imagename, title, "Step 4:new page are not opened");
Filel.changelastgreentored (SB);
Filel.addlog (folder + "/" + file, SB);
Common.capturescreen (Firfox, Folderimage, imagename, "PNG");
System.out.println ("Step 4:new page isn't opened");
}//abc.click ();
Firfox.close ();
The public static void second () throws interruptedexception {File File = new file ("src/html_file/ding_wei.html");
String FilePath = "file:///" + File.getabsolutepath ();
System.out.printf ("Now accesss%s \ n", FilePath);
Firfox.get (FilePath);
Thread.Sleep (2000);
by id webelement mail = firfox.findelement (by.id ("Inputemail"));
Mail.click ();
Thread.Sleep (2000);
Mail.sendkeys ("email name");
Thread.Sleep (2000);
by name webelement pwd = firfox.findelement (by.name ("password"));
Pwd.sendkeys ("Passwod");
Thread.Sleep (2000); by tagname String classofform = firfox.findelement (By.tagname ("form"). GetAttribute ("class");
System.out.printf ("%s\n", classofform);
Thread.Sleep (2000);
By link text webelement link = firfox.findelement (by.linktext ("register");
((Javascriptexecutor) firfox). Executescript ("$ (arguments[0)). fadeout (). FadeIn ()", link);
Thread.Sleep (2000);
Link.click ();
Thread.Sleep (2000);
By link CALSS webelement submit = Firfox.findelement (By.classname ("btn"));
Submit.click (); }
}