Logger.info 格式化
Logger.info formatting
我正在尝试记录我的测试发现的内容,但它每次都会记录时间和日期。我需要对日志进行排列,以便它可以读取:
"[Date/Time] User number 1 name Test (Pass or Fail)
Button (Found Not or Found) ... ... ... ...
User number 2 name Test ... ... ... ... ... ...
User number 3 name Test ...
... ... ... ... ..."
这是我当前的代码:
package com.centurylink.rss.test;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.google.common.base.Predicate;
public class checkaccess {
private static final Logger logger = Logger.getLogger("AccessTest");
private static FileHandler fh;
public static void main(String[] args) {
try {
// This block configure the logger with handler and
// formatter
fh = new FileHandler("C:/SeleniumLogFiles/AccessTest.log", true);
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
// The following statement is used to log any
// messages
logger.info("Button Test");
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Declaration and instantiation of objects/variables
String actualTitle = "";
List<WebElement> actualButtonlist;
Set<String> buttons = new HashSet<>();
Map<String[], Set<String>> map = new HashMap<String[], Set<String>>();
String[] u1 = { "firstname lastname", "username" };
String[] u2 = { "firstname lastname", "username" };
String[] u3 = { "firstname lastname", "username" };
String[] u4 = { "firstname lastname", "username" };
String[] u5 = { "firstname lastname", "username" };
String[] u6 = { "firstname lastname", "username" };
String[] u7 = { "firstname lastname", "username" };
String[] u8 = { "firstname lastname", "username" };
String[] u9 = { "firstname lastname", "username" };
String[] u10 = { "firstname lastname", "username" };
String[] u11 = { "firstname lastname", "username" };
String[] u12 = { "firstname lastname", "username" };
String[] u13 = { "firstname lastname", "username" };
String[] u14 = { "firstname lastname", "username" };
buttons.add("Home");
buttons.add("Subscriptions");
buttons.add("Content");
buttons.add("Add Content");
buttons.add("Review");
buttons.add("Channels");
buttons.add("Add Channel");
buttons.add("Review");
buttons.add("Groups");
buttons.add("Add Group");
buttons.add("Review");
buttons.add("Users");
buttons.add("Tools");
buttons.add("1 Call Guide");
buttons.add("2 Call Guide");
buttons.add("3 Call Guide");
buttons.add("Mart");
buttons.add("Library");
buttons.add("Place");
buttons.add("Tracking Report");
map.put(u1, buttons);
for (String[] d : map.keySet()) {
map.get(d);
}
String[] accessnames = new String[14];
accessnames[0] = "firstname lastname";
accessnames[1] = "firstname lastname";
accessnames[2] = "firstname lastname";
accessnames[3] = "firstname lastname";
accessnames[4] = "firstname lastname";
accessnames[5] = "firstname lastname";
accessnames[6] = "firstname lastname";
accessnames[7] = "firstname lastname";
accessnames[8] = "firstname lastname";
accessnames[9] = "firstname lastname";
accessnames[10] = "firstname lastname";
accessnames[11] = "firstname lastname";
accessnames[12] = "firstname lastname";
accessnames[13] = "firstname lastname";
String[] values = new String[14];
values[0] = "username";
values[1] = "username";
values[2] = "username";
values[3] = "username";
values[4] = "username";
values[5] = "username";
values[6] = "username";
values[7] = "username";
values[8] = "username";
values[9] = "username";
values[10] = "username";
values[11] = "username";
values[12] = "username";
values[13] = "username";
WebDriver driver = new FirefoxDriver();
// Initializes Loop
int i = values.length;
for (int j = 0; j < i; j++) {
// Declaration and instantiation of objects/variables
String baseUrl = "http://adress_here.com";
String expectedTitle = "Welcome " + accessnames[j];
// Launch Firefox and direct it to the Base URL
driver.get(baseUrl);
// Get the text box element to type into & click submit
WebElement userid = driver.findElement(By.id("username"));
WebElement submit = driver.findElement(By.xpath("//*[@value='Submit']"));
// Input into text box
Actions builder = new Actions(driver);
Action InputId = builder.moveToElement(userid).click().sendKeys(userid, values[j]).moveToElement(submit)
.click().build();
InputId.perform();
// Waits 3 seconds for page to load
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get the actual buttons of the title & For Each Loop
// int x = Buttons.length;
actualButtonlist = driver.findElements(By.xpath("//div[@id='site']/input"));
int x = actualButtonlist.size();
// Log access pass or fail
if (actualTitle.contentEquals(expectedTitle)) {
logger.info(accessnames[j] + " Access Test Pass");
} else {
logger.info(accessnames[j] + " Access Test Failed");
}
// Prints pass or fail to the console
// if (actualTitle.contentEquals(expectedTitle))
// {
// System.out.println("Login Test Passed!");
// } else {
// System.out.println("Login Test Failed");
// }
// for (WebElement actualButton : actualButtonlist) {
Set<String> thingsFound = checkFor(actualButtonlist, buttons);
for (String s : buttons) {
if (thingsFound.contains(s)) {
logger.info(s + " found");
} else {
logger.info(s + " not found");
}
}
// Get the actual value of the title
actualTitle = driver.findElement(By.id("userInfoTitle")).getText();
driver.manage().deleteAllCookies();
}
// }
try {
fileopener();
} catch (Exception e) {
}
driver.close();
}
public static Set<String> checkFor(List<WebElement> web, Set<String> things) {
String temp = null;
HashSet<String> s = new HashSet<String>();
for (WebElement w : web) {
temp = w.getAttribute("value");
if (things.contains(temp)) {
s.add(temp);
}
}
return s;
}
public static void fileopener() throws IOException {
WebDriver driver = new FirefoxDriver();
driver.get("file:C:\SeleniumLogFiles\AccessTest.log");
// text file, should be opening in default text editor
File file = new File("C:/SeleniumLogFiles/AccessTest.log");
// first check if Desktop is supported by Platform or not
if (!Desktop.isDesktopSupported()) {
System.out.println("Desktop is not supported");
return;
}
Desktop desktop = Desktop.getDesktop();
if (file.exists()) {
desktop.open(file);
}
}
}
您似乎想将 java.util.logging.SimpleFormatter.format 设置为:
java.util.logging.SimpleFormatter.format=%5$s %6$s%n
并在日志语句中记录 current date and time:
if (actualTitle.contentEquals(expectedTitle)) {
logger.log(Level.INFO, "{0} Access Test Pass {1,date} {1,time}",
new Object[]{accessnames[j] , System.currentTimeMillis()});
} else {
logger.log(Level.INFO, "{0} Access Test Failed {1,date, EEE, MMM dd HH:mm:ss:S ZZZ yyyy}",
new Object[]{accessnames[j] , System.currentTimeMillis()});
}
我正在尝试记录我的测试发现的内容,但它每次都会记录时间和日期。我需要对日志进行排列,以便它可以读取:
"[Date/Time] User number 1 name Test (Pass or Fail)
Button (Found Not or Found) ... ... ... ...
User number 2 name Test ... ... ... ... ... ...
User number 3 name Test ...
... ... ... ... ..."
这是我当前的代码:
package com.centurylink.rss.test;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.google.common.base.Predicate;
public class checkaccess {
private static final Logger logger = Logger.getLogger("AccessTest");
private static FileHandler fh;
public static void main(String[] args) {
try {
// This block configure the logger with handler and
// formatter
fh = new FileHandler("C:/SeleniumLogFiles/AccessTest.log", true);
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
// The following statement is used to log any
// messages
logger.info("Button Test");
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Declaration and instantiation of objects/variables
String actualTitle = "";
List<WebElement> actualButtonlist;
Set<String> buttons = new HashSet<>();
Map<String[], Set<String>> map = new HashMap<String[], Set<String>>();
String[] u1 = { "firstname lastname", "username" };
String[] u2 = { "firstname lastname", "username" };
String[] u3 = { "firstname lastname", "username" };
String[] u4 = { "firstname lastname", "username" };
String[] u5 = { "firstname lastname", "username" };
String[] u6 = { "firstname lastname", "username" };
String[] u7 = { "firstname lastname", "username" };
String[] u8 = { "firstname lastname", "username" };
String[] u9 = { "firstname lastname", "username" };
String[] u10 = { "firstname lastname", "username" };
String[] u11 = { "firstname lastname", "username" };
String[] u12 = { "firstname lastname", "username" };
String[] u13 = { "firstname lastname", "username" };
String[] u14 = { "firstname lastname", "username" };
buttons.add("Home");
buttons.add("Subscriptions");
buttons.add("Content");
buttons.add("Add Content");
buttons.add("Review");
buttons.add("Channels");
buttons.add("Add Channel");
buttons.add("Review");
buttons.add("Groups");
buttons.add("Add Group");
buttons.add("Review");
buttons.add("Users");
buttons.add("Tools");
buttons.add("1 Call Guide");
buttons.add("2 Call Guide");
buttons.add("3 Call Guide");
buttons.add("Mart");
buttons.add("Library");
buttons.add("Place");
buttons.add("Tracking Report");
map.put(u1, buttons);
for (String[] d : map.keySet()) {
map.get(d);
}
String[] accessnames = new String[14];
accessnames[0] = "firstname lastname";
accessnames[1] = "firstname lastname";
accessnames[2] = "firstname lastname";
accessnames[3] = "firstname lastname";
accessnames[4] = "firstname lastname";
accessnames[5] = "firstname lastname";
accessnames[6] = "firstname lastname";
accessnames[7] = "firstname lastname";
accessnames[8] = "firstname lastname";
accessnames[9] = "firstname lastname";
accessnames[10] = "firstname lastname";
accessnames[11] = "firstname lastname";
accessnames[12] = "firstname lastname";
accessnames[13] = "firstname lastname";
String[] values = new String[14];
values[0] = "username";
values[1] = "username";
values[2] = "username";
values[3] = "username";
values[4] = "username";
values[5] = "username";
values[6] = "username";
values[7] = "username";
values[8] = "username";
values[9] = "username";
values[10] = "username";
values[11] = "username";
values[12] = "username";
values[13] = "username";
WebDriver driver = new FirefoxDriver();
// Initializes Loop
int i = values.length;
for (int j = 0; j < i; j++) {
// Declaration and instantiation of objects/variables
String baseUrl = "http://adress_here.com";
String expectedTitle = "Welcome " + accessnames[j];
// Launch Firefox and direct it to the Base URL
driver.get(baseUrl);
// Get the text box element to type into & click submit
WebElement userid = driver.findElement(By.id("username"));
WebElement submit = driver.findElement(By.xpath("//*[@value='Submit']"));
// Input into text box
Actions builder = new Actions(driver);
Action InputId = builder.moveToElement(userid).click().sendKeys(userid, values[j]).moveToElement(submit)
.click().build();
InputId.perform();
// Waits 3 seconds for page to load
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get the actual buttons of the title & For Each Loop
// int x = Buttons.length;
actualButtonlist = driver.findElements(By.xpath("//div[@id='site']/input"));
int x = actualButtonlist.size();
// Log access pass or fail
if (actualTitle.contentEquals(expectedTitle)) {
logger.info(accessnames[j] + " Access Test Pass");
} else {
logger.info(accessnames[j] + " Access Test Failed");
}
// Prints pass or fail to the console
// if (actualTitle.contentEquals(expectedTitle))
// {
// System.out.println("Login Test Passed!");
// } else {
// System.out.println("Login Test Failed");
// }
// for (WebElement actualButton : actualButtonlist) {
Set<String> thingsFound = checkFor(actualButtonlist, buttons);
for (String s : buttons) {
if (thingsFound.contains(s)) {
logger.info(s + " found");
} else {
logger.info(s + " not found");
}
}
// Get the actual value of the title
actualTitle = driver.findElement(By.id("userInfoTitle")).getText();
driver.manage().deleteAllCookies();
}
// }
try {
fileopener();
} catch (Exception e) {
}
driver.close();
}
public static Set<String> checkFor(List<WebElement> web, Set<String> things) {
String temp = null;
HashSet<String> s = new HashSet<String>();
for (WebElement w : web) {
temp = w.getAttribute("value");
if (things.contains(temp)) {
s.add(temp);
}
}
return s;
}
public static void fileopener() throws IOException {
WebDriver driver = new FirefoxDriver();
driver.get("file:C:\SeleniumLogFiles\AccessTest.log");
// text file, should be opening in default text editor
File file = new File("C:/SeleniumLogFiles/AccessTest.log");
// first check if Desktop is supported by Platform or not
if (!Desktop.isDesktopSupported()) {
System.out.println("Desktop is not supported");
return;
}
Desktop desktop = Desktop.getDesktop();
if (file.exists()) {
desktop.open(file);
}
}
}
您似乎想将 java.util.logging.SimpleFormatter.format 设置为:
java.util.logging.SimpleFormatter.format=%5$s %6$s%n
并在日志语句中记录 current date and time:
if (actualTitle.contentEquals(expectedTitle)) {
logger.log(Level.INFO, "{0} Access Test Pass {1,date} {1,time}",
new Object[]{accessnames[j] , System.currentTimeMillis()});
} else {
logger.log(Level.INFO, "{0} Access Test Failed {1,date, EEE, MMM dd HH:mm:ss:S ZZZ yyyy}",
new Object[]{accessnames[j] , System.currentTimeMillis()});
}