Come in and stay in the basic theory of knowledge learning, think too boring, wrote a Web page of their own initiative of the demo: their own initiative to write daily. The province later itself opens the webpage to write.
Directly on the code:
I volunteered to fill out the 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 w Ebsite ";p rivate static int mhours = 8;private static final String content_file = System.getproperty (" User.dir ") + File.sepa Rator + "config" + file.separator + "Logcontent.txt";p rivate static final String driver_file = System.getproperty ("User.di R ") + File.separator +" Driver " + File.separator + "IEDriverServer.exe";p rivate static string mcontext = "Job:";p rivate static string mdate = "1900-1-1" ;p rivate webdriver mdriver = null;private String mflag = "FAIL"; @Overridepublic void SetUp () throws Exception {Calculateho Urs (); config ();} @Testpublic 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 = h Our-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 Blocke.printstacktrace ();} catch ( FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Auto-gene Rated catch Blocke.printstacktrace ();} finally {Mcontext + = sb;try {br.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.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 Blocke.printstacktrace ();} catch (Messagingexception e) {//TODO Auto-generated Catch Blocke.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. So I need to configure the driver for IE in the code.
The premise is to go down to the local. 32-bit and 64-bit, I just started due to the wrong 64-bit, always start the browser, tossing a very 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 folder 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:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvaxrmb290ymfsba==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
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.
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvaxrmb290ymfsba==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
Then press the cursor button in the tool and click on the control you want to see.
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvaxrmb290ymfsba==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
So the above three selection boxes are positioned by ID.
To navigate down the time input box:
Webelement Text = mdriver.findelement (by.id ("Field_costtime")); Text.sendkeys (mhours + "");
This is very easy. The ID to locate. The calculation of time is calculated by real-time. Take 9 points to work. Calculates the difference between the current time and the 9 point.
Each company's rules and regulations vary, and each company calculates a different 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.
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvaxrmb290ymfsba==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
But positioning the daily input box is a bit troublesome. Because it is present in the IFRAME. Not directly located.
So first you have to switch to the IFRAME, and an IFRAME is included in the Contentframe. And then you have to turn. This is the time to really turn to the class named Ke-edit-iframe.
Then position 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 first. Then locate the Submit button.
Mdriver.switchto (). Defaultcontent (); Webelement submit = Mdriver.findelement (By.name ("Submit2")); Submit.click ();
Well, this is the end of the step.
But as a case. Nature must have checkpoint. So I added an inference to the back of the code to infer if I had successfully filled out the daily and then sent 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);
Selenium's Lazy tutorial