请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

控制台中没有log4j的输出信息,error.log文件中也没有任何信息

图片描述
[RemoteTestNG] detected TestNG version 6.14.3
beforeClass+++++++++++++
Starting ChromeDriver 2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) on port 45926
Only local connections are allowed.
8月 10, 2020 3:01:04 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: OSS
执行case1
[DEBUG] 2020-08-10 15:01:15,767 method:com.mushishi.Imooc.testngCase.case1(testngCase.java:71)
邮箱正确
执行case2
用例失败了…
截图地址为:D:\work\eclipse-workspace\Imooc\com.mushishi.Imooc.TestNGLisinScreen_2020_08_10_15_01_16.png
执行case3
[DEBUG] 2020-08-10 15:01:16,470 method:com.mushishi.Imooc.testngCase.case3(testngCase.java:86)
密码正确
执行case4
用例失败了…
截图地址为:D:\work\eclipse-workspace\Imooc\com.mushishi.Imooc.TestNGLisinScreen_2020_08_10_15_01_16.png
执行case5
[DEBUG] 2020-08-10 15:01:17,025 method:com.mushishi.Imooc.testngCase.case5(testngCase.java:101)
登录正确
[DEBUG] 2020-08-10 15:01:17,031 method:com.mushishi.Imooc.testngCase.f(testngCase.java:63)
f成功
执行f
afterClass-------------
PASSED: case1
PASSED: case3
PASSED: case5
PASSED: f
FAILED: case2
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:“name”,“selector”:“email1”}
(Session info: chrome=65.0.3325.162)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: ‘3.141.0’, revision: ‘2ecb7d9a’, time: '2018-10-31T20:09:30’
System info: host: ‘MS-20130206AGNW’, ip: ‘192.168.0.7’, os.name: ‘Windows 7’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: '12.0.1’
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.38.552522 (437e6fbedfa876…, userDataDir: C:\Users\ADMINI~1\AppData\L…}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 65.0.3325.162, webStorageEnabled: true}
Session ID: 82a28e585d7ee42f873e59fc232581e7
*** Element info: {Using=name, value=email1}
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:404)
at org.openqa.selenium.By$ByName.findElement(By.java:284)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at com.mushishi.Imooc.testngCase.case2(testngCase.java:78)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

FAILED: case4
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:“name”,“selector”:“password1”}
(Session info: chrome=65.0.3325.162)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: ‘3.141.0’, revision: ‘2ecb7d9a’, time: '2018-10-31T20:09:30’
System info: host: ‘MS-20130206AGNW’, ip: ‘192.168.0.7’, os.name: ‘Windows 7’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: '12.0.1’
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.38.552522 (437e6fbedfa876…, userDataDir: C:\Users\ADMINI~1\AppData\L…}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 65.0.3325.162, webStorageEnabled: true}
Session ID: 82a28e585d7ee42f873e59fc232581e7
*** Element info: {Using=name, value=password1}
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:404)
at org.openqa.selenium.By$ByName.findElement(By.java:284)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at com.mushishi.Imooc.testngCase.case4(testngCase.java:92)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

===============================================
Default test
Tests run: 6, Failures: 2, Skips: 0

===============================================
Default suite
Total tests run: 6, Failures: 2, Skips: 0

图片描述

图片描述

请问老师,为什么按照视频中的内容,出现的结果却是在控制台中显示的log4j只有debug的输出而没有error的输出呢?而且error.log文件中也没有任何信息记录,这个该如何解决呢?

java代码如下:
package com.mushishi.Imooc;

import org.testng.annotations.Test;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Listeners;
import org.testng.annotations.AfterTest;

@Listeners({TestNGLisinScreen.class})

public class testngCase {

private static Logger logger = Logger.getLogger(testngCase.class);

public WebDriver driver;

public void initDriver() {
	driver = new ChromeDriver();
	driver.get("https://www.imooc.com/");
	try {
		Thread.sleep(3000);
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	driver.manage().window().maximize();
	try {
		Thread.sleep(2000);
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	driver.findElement(By.id("js-signin-btn")).click();
	try {
		Thread.sleep(2000);
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}

public void sendEmail() {

  SimpleEmail email = new SimpleEmail();
  email.setHostName("smtp.163.com");
  email.setAuthentication("yxzxm156@163.com", "xxxxx");
  try {
	email.setFrom("yxzxm156@163.com");
	email.addTo("979212185@qq.com");
	email.setSubject("selenium subject");
	email.setMsg("this is test");
	email.send();
	logger.debug("邮件发送成功");
} catch (EmailException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}  

}

@Test
public void f() {
logger.debug(“f成功”);
System.out.println(“执行f”);
}

@Test
public void case1() {
System.out.println(“执行case1”);
driver.findElement(By.name(“email”)).sendKeys(“18156567785”);
logger.debug(“邮箱正确”);
// Assert.assertEquals(1, 2);
}

@Test
public void case2() {
System.out.println(“执行case2”);
driver.findElement(By.name(“email1”)).sendKeys(“18156567785”);
logger.error(“邮箱错误”);
}

@Test
public void case3() {
System.out.println(“执行case3”);
driver.findElement(By.name(“password”)).sendKeys(“1234567890”);
logger.debug(“密码正确”);
}

@Test
public void case4() {
System.out.println(“执行case4”);
driver.findElement(By.name(“password1”)).sendKeys(“1234567890”);
logger.error(“密码错误”);
}

@Test
public void case5() {
System.out.println(“执行case5”);
driver.findElement(By.className(“moco-btn-red”)).click();
logger.debug(“登录正确”);
}

@BeforeMethod
public void beforeMethod() {
}

@AfterMethod
public void afterMethod() {
}

@BeforeClass
public void beforeClass() {
System.out.println(“beforeClass+++++++++++++”);
PropertyConfigurator.configure(“log4j.properties”);
initDriver();
}

@AfterClass
public void afterClass() {
System.out.println(“afterClass-------------”);
driver.close();
}

@BeforeTest
public void beforeTest() {
}

@AfterTest
public void afterTest() {
}

}

log4j文件内容如下:

\u8BBE\u7F6E###

log4j.rootLogger = debug,stdout,D,E

\u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u62AC

log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

\u8F93\u51FADEBUG \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u6587\u4EF6\u8BBE\u7F6E

log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = D:\work\eclipse-workspace\Imooc\log\debug.log
log4j.appender.D.Append = true
log4j.appender.D.Encoding= UTF8
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} - [ %5p ] %l %t %c - %m %n

\u8F93\u51FAERROR \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u6587\u4EF6\u8BBE\u7F6E

log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = D:\work\eclipse-workspace\Imooc\log\error.log
log4j.appender.E.Append = true
log4j.appender.E.Encoding= UTF8
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern =%-d{yyyy-MM-dd HH:mm:ss} - [ %5p ] %l %t %c - %m %n

正在回答 回答被采纳积分+3

3回答

Mushishi 2020-08-10 15:19:34

你代码发一下呀。

1 回复 有任何疑惑可以回复我~
  • 提问者 qq_醉朴_156 #1
    好的,老师,Java代码和log4j文件内容都已经重新上传了。
    回复 有任何疑惑可以回复我~ 2020-08-10 16:24:56
  • Mushishi 回复 提问者 qq_醉朴_156 #2
    你这个我看着是没问题的,如果确实还有问题,不行你替换成下面这个试一下。或者把你的每日单独生成一个日志文件哪个更改为 就用一个文件
    回复 有任何疑惑可以回复我~ 2020-08-11 17:32:55
  • 提问者 qq_醉朴_156 回复 Mushishi #3
    好的,谢谢老师,我试一下
    回复 有任何疑惑可以回复我~ 2020-08-11 22:08:45
Mushishi 2020-08-11 17:33:28
 ### set log levels ###
log4j.rootLogger = debug ,  stdout ,  D ,  E

### 输出到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =  %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n

### 输出到日志文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG ## 输出DEBUG级别以上的日志
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 保存异常信息到单独文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/error.log ## 异常日志文件名
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR ## 只输出ERROR级别以上的日志!!!
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n


0 回复 有任何疑惑可以回复我~
  • 提问者 qq_醉朴_156 #1
    老师,我找到答案了,是因为我的定位元素出了问题,然后报错了,后面的logger.error()也就不执行了。
    回复 有任何疑惑可以回复我~ 2020-08-11 22:38:30
Mushishi 2020-08-10 15:26:00

配置文件看着没问题,如果不是很理解配置文件得内容 可以https://www.jianshu.com/p/ccafda45bcea看一下这个。如果还是不对,你把代码发一下。用一个最简单得java方法去调试打印

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信