Выполните следующие шаги:
1) Создайте новый XML-файл– log4j.xml и поместите его в корневую директорию проекта.
2) Скопируйте следующий код в созданный файл:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false"> <appender name="fileAppender" class="org.apache.log4j.FileAppender"> <param name="Threshold" value="INFO" /> <param name="File" value="logfile.log"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p [%c{1}] %m %n" /> </layout> </appender> <root> <level value="INFO"/> <appender-ref ref="fileAppender"/> </root> </log4j:configuration> |
Примечание: После копирования кода обратите внимание, что код остался без изменений. Копирование с HTML может изменить некоторые символы, например (“) на (?)
3) Теперь внесите следующий код в ранее созданный скрипт или создайте новый класс.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
package automationFramework; import java.util.concurrent.TimeUnit; import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Log4j { private static WebDriver driver; private static Logger Log = Logger.getLogger(Log4j.class.getName()); public static void main(String[] args) { DOMConfigurator.configure("log4j.xml"); // Create a new instance of the Firefox driver driver = new FirefoxDriver(); Log.info("New driver instantiated"); //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); Log.info("Implicit wait applied on the driver for 10 seconds"); //Launch the Online Store Website driver.get("http://www.onlinestore.toolsqa.com"); Log.info("Web application launched"); // Find the element that's ID attribute is 'account'(My Account) driver.findElement(By.id("account")).click(); Log.info("Click action performed on My Account link"); // Find the element that's ID attribute is 'log' (Username) // Enter Username on the element found by above desc. driver.findElement(By.id("log")).sendKeys("testuser_1"); Log.info("Username entered in the Username text box"); // Find the element that's ID attribute is 'pwd' (Password) // Enter Password on the element found by the above desc. driver.findElement(By.id("pwd")).sendKeys("Test@123"); Log.info("Password entered in the Password text box"); // Now submit the form. WebDriver will find the form for us from the element driver.findElement(By.id("login")).click(); Log.info("Click action performed on Submit button"); // Print a Log In message to the screen System.out.println(" Login Successfully, now it is the time to Log Off buddy."); // Find the element that's ID attribute is 'account_logout' (Log Out) driver.findElement(By.id("account_logout")); Log.info("Click action performed on Log out link"); // Close the driver driver.quit(); Log.info("Browser closed"); } } |
4) Добавьте пустой “logfile.txt” в корень сайта.
5) Запустите скрипт с использованием команды Run As -> Java Application.
6) После выполнения крипта откройте файл “logfile.txt” и просмотрите логирование.