Come in always stay in the basic theory of knowledge learning, feel too boring, wrote a Web Automation demo: automatic writing daily. The province later itself opens the webpage to write.
Directly on the code:
Auto Fill Daily demo
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import java.io.UnsupportedEncodingException;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date;
Import javax.mail.MessagingException;
Import javax.mail.internet.AddressException;
Import Junit.framework.TestCase;
Import Org.junit.Test;
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import Org.openqa.selenium.ie.InternetExplorerDriver;
Import Org.openqa.selenium.support.ui.Select;
public class AutoLog extends TestCase {private static final String URL = "Your log website";
private static int mhours = 8; private static final String Content_file = System.getproperty ("User.dir") + file.separator + "config" + file.separator
+ "Logcontent.txt"; private static final String Driver_file = System.getproperty ("UsEr.dir ") + file.separator +" Driver "+ file.separator +" IEDriverServer.exe ";
private static String Mcontext = "Job:";
private static String mdate = "1900-1-1";
Private Webdriver mdriver = null;
Private String Mflag = "FAIL";
@Override public void SetUp () throws Exception {calculatehours ();
Config ();
} @Test public void Test_writelog () {try {system.setproperty ("Webdriver.ie.driver", driver_file);
MDRIVer = new Internetexplorerdriver ();
Mdriver.get (URL);
Select select = New Select (Mdriver.findelement (by. ID ("Task_productname")));
Select.selectbyvisibletext ("ITest");
Select Select1 = new Select (Mdriver.findelement (by. ID ("Field_class_1"));
Select1.selectbyvisibletext ("Test_development");
Select select2 = new Select (Mdriver.findelement (by. ID ("field_class_2"));
Select2.selectbyvisibletext ("Coding");
Webelement Text = mdriver.findelement (by.id ("Field_costtime"));
Text.sendkeys (mhours + ""); Mdriver.switchto (). fraMe ("Contentframe");
Mdriver.switchto (). frame (0);
Webelement content = MDRIVer. Findelement (By.classname ("ke-content"));
Content.click ();
Content.sendkeys (Mcontext);
Mdriver.switchto (). Defaultcontent ();
Webelement submit = Mdriver.findelement (By.name ("Submit2"));
Submit.click ();
Webelement table =//Mdriver.findelements (By.id ("SubmitForm")). Get (1)//. Findelement (By.id ("mytodotable"));
Webelement table = mdriver.findelement (By.id ("This_last")). Findelement (By.tagname ("table"));
webelement tr = table.findelement (By.tagname ("Tbody")). Findelement (By.tagname ("tr"));
if (Mdate equals (Tr.findelements ("By.tagname (" TD) "). Get (1). GetText ())) {Mflag =" success["+ tr.gettext () +"] ";
}} catch (Exception e) {e.printstacktrace ();
Mflag = "EXCEPTION";
} finally {SendMail (mflag);
}} public void TearDown () throws Exception {mdriver.quit (); public void Calculatehours () {Date date = new Date ();
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd");
Mdate = Df.format (date);
Calendar C = calendar.getinstance ();
int hour = C.get (Calendar.hour_of_day);
int minute = C.get (Calendar.minute);
Mhours = hour-9-1; minute = minute < 30?
0:1;
Mhours + = minute; Mhours = Mhours < 8?
8:mhours;
} public void Config () {StringBuilder sb = new StringBuilder ();
BufferedReader br = null;
try {br = new BufferedReader (new InputStreamReader (New FileInputStream (Content_file), "UTF-8"));
String line = null;
while (line = Br.readline ())! = null) {sb.append (line);
}} catch (Unsupportedencodingexception e) {//TODO auto-generated catch block E.printstacktrace ();
} catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
} finally {mcontext + = SB;
try {br.close (); } catch (IoexcePtion e) {//TODO auto-generated catch block E.printstacktrace ();
}}} public void SendMail (String result) {MailSender javaemail = new MailSender ();
Javaemail.setmailserverproperties ();
try {javaemail.createemailmessage (result);
Javaemail.sendemail ();
} catch (Addressexception e) {//TODO auto-generated catch block E.printstacktrace ();
} catch (Messagingexception e) {//TODO auto-generated catch block E.printstacktrace ();
}
}
}
Let's talk about the process.
1. Create a new Java project
Import Selenium-server-standalone-2.42.2.jar, Mailapi.jar, Smtp.jar, and junit libraries. The first jar package is a selenium-webdriver dependent jar package, and the latter two are for sending mail.
2. Download Iedriverserver
Because I use IE, I need to configure the driver of IE in the code. The premise is to go down to the local. 32-bit and 64-bit, I just started because of the wrong 64-bit, always start the browser, tossing a long time. So watch the machine and download it again. I've uploaded 32-bit iedriverserver here.
3. Code writing
Start the browser first: The first step is to set your Iedriverserver directory to the environment variable, then launch the browser and enter the URL.
System.setproperty ("Webdriver.ie.driver", driver_file);
MDRIVer = new Internetexplorerdriver ();
Mdriver.get (URL);
Wait for the page to load and it will look like this:
And then I'm going to locate the control I want to select and fill in: First 3 selection boxes, navigate through the Selenium Select object and select the one I want,
Select select = New Select (Mdriver.findelement (by.id ("Task_productname"));
Select.selectbyvisibletext ("ITest");
Select Select1 = new Select (Mdriver.findelement (by.id ("Field_class_1"));
Select1.selectbyvisibletext ("Test_development");
Select select2 = new Select (Mdriver.findelement (by.id ("field_class_2"));
Select2.selectbyvisibletext ("Coding");
View the page control elements by pressing F12 on the current page.
Then press the cursor button in the tool and click on the control you want to see.
So the above three selection boxes are positioned by ID.
Below to locate the time input box:
Webelement Text = mdriver.findelement (by.id ("Field_costtime"));
Text.sendkeys (mhours + "");
This is easy to locate by ID. The calculation of time is calculated by real-time, the difference between the current time and 9 points is calculated by 9 points of work. Each company's rules and regulations vary, and each company calculates the same way.
Then fill in the daily content, I use the way is to write the contents of the daily paper in a TXT document, and then read this document write.
But locating the daily input box is a bit cumbersome because it is in the IFRAME and is not directly located.
So first you have to switch to the IFRAME, and the contentframe contains an IFRAME, and then you have to go. This is the time to really turn to the class named Ke-edit-iframe. Before you can locate the input box.
Mdriver.switchto (). FRAME ("Contentframe");
Mdriver.switchto (). frame (0);
Webelement content = mdriver.findelement (By.classname ("ke-content"));
Content.click ();
Content.sendkeys (Mcontext);
Fill in the contents of the above will be submitted. But the submit button is not in the IFRAME just now.
So you have to switch to the current context before you locate the submit button.
Mdriver.switchto (). Defaultcontent ();
Webelement submit = Mdriver.findelement (By.name ("Submit2"));
Submit.click ();
Well, it's done by this step. But as a case, naturally want to have checkpoint, so I put a judgment on the back of the code, determine whether I successfully fill in the daily, and then send the results to my mailbox.
Webelement table = mdriver.findelement (By.id ("This_last"))
. Findelement (By.tagname ("table"));
webelement tr = table.findelement (By.tagname ("Tbody")). Findelement (
by.tagname ("tr"));
if (mdate
equals (Tr.findelements ("By.tagname (" TD) "). Get (1). GetText ())) {
Mflag =" success["+ tr.gettext () +" ]";
}
SendMail (Mflag);