Webdriver---API---(Java Edition) advanced applications 3

Source: Internet
Author: User
Tags testng

Use log4j to print the execution log during testing

1. Create a Log4j.xml configuration file

<?XML version= "1.0" encoding= "UTF-8"?>    <!DOCTYPE log4j:configuration SYSTEM "Log4j.dtd"><log4j:configurationxmlns:log4j= "http://jakarta.apache.org/log4j/"Debug= "false">  <Appendername= "Fileappender"class= "Org.apache.log4j.FileAppender">  <paramname= "Threshold"value= "INFO"/>  <paramname= "File"value= "Logfile.log"/>  <Layoutclass= "Org.apache.log4j.PatternLayout">            <paramname= "Conversionpattern"value= "%d%-5p [%c{1}]%m%n"/>  </Layout>></Appender><Root>  < Levelvalue= "INFO"/>  <Appender-refref= "Fileappender"/></Root></log4j:configuration>

2, write the tool class code

 PackageChina ;ImportOrg.apache.log4j.Logger; Public classLog {Private StaticLogger Log=logger.getlogger (Log.class. GetName ());  Public Static voidstarttestcase (String stestcasename) {log.info ("-------------------------------------------"); Log.info ("*************" +stestcasename+ "*************"); }     Public Static voidendtestcase (String stestcasename) {log.info ("**************** test Case execution End ****************"); Log.info ("-------------------------------------------"); }     Public Static voidinfo (String message) {log.info (message); }     Public Static voidwarn (String message) {log.info (message); }     Public Static voiderror (String message) {log.info (message); }     Public Static voidFatal (String message) {log.info (message); }     Public Static voidDebug (String message) {log.info (message); }}

3. Write test Class Code

 PackageChina ;Importorg.testng.annotations.Test;ImportOrg.testng.annotations.BeforeMethod;ImportOrg.apache.log4j.xml.DOMConfigurator;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;ImportOrg.openqa.selenium.chrome.ChromeDriver;ImportOrg.testng.annotations.AfterMethod;ImportOrg.testng.annotations.BeforeClass; Public classtestlog4j { PublicWebdriver Driver; String BASEURL= "Http://www.sogou.com"; @Test Public voidf () {Log.starttestcase (Search);      Driver.get (BASEURL); Log.info ("Open Sogou Home"); Driver.findelement (By.id ("Query"). SendKeys ("Automated test")); Log.info ("Enter search keywords" automated test ""); Driver.findelement (By.id ("STB") . Click (); Log.info ("Stand-alone Search button"); Log.endtestcase (Search); } @BeforeMethod Public voidBeforemethod () {System.setproperty ("Webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe"); Driver=NewChromedriver (); } @AfterMethod Public voidAftermethod () {driver.quit (); } @BeforeClass Public voidBeforeclass () {domconfigurator.configure ("Log4j.xml"); }}

Webdriver---API---(Java Edition) advanced applications 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.