Log4j未记录来自“If else”条件内部的数据(Java Selenium Web驱动程序)

u3r8eeie  于 2022-11-06  发布在  Java
关注(0)|答案(1)|浏览(125)

我遇到了Log4j问题。当我运行下面的测试用例时,输出正常,所有测试都已通过。但Log4j未正确记录。它仅记录“If else”条件外部的数据,而跳过“If else”条件内部的数据。
我的测试脚本:

  1. package com.LoginPage.Testcase;
  2. import java.io.IOException;
  3. import java.util.concurrent.TimeUnit;
  4. import org.apache.log4j.Logger;
  5. import org.apache.log4j.PropertyConfigurator;
  6. import org.openqa.selenium.By;
  7. import org.openqa.selenium.WebDriver;
  8. import org.openqa.selenium.WebElement;
  9. import org.openqa.selenium.chrome.ChromeDriver;
  10. import org.openqa.selenium.firefox.FirefoxDriver;
  11. import org.openqa.selenium.ie.InternetExplorerDriver;
  12. import org.openqa.selenium.support.ui.WebDriverWait;
  13. import org.testng.Assert;
  14. import org.testng.annotations.AfterClass;
  15. import org.testng.annotations.BeforeClass;
  16. import org.testng.annotations.DataProvider;
  17. import org.testng.annotations.Parameters;
  18. import org.testng.annotations.Test;
  19. public class LoginPageDDT {
  20. public static WebDriver driver;
  21. public static Logger logger;
  22. @Parameters("browser")
  23. @BeforeClass
  24. public void setup(String br)
  25. {
  26. logger = Logger.getLogger("LoginPage");
  27. PropertyConfigurator.configure("Log4j.properties");
  28. if(br.equals("chrome"))
  29. {
  30. System.setProperty("webdriver.chrome.driver", "./Drivers\\chromedriver.exe");
  31. driver=new ChromeDriver();
  32. }
  33. else if(br.equals("firefox"))
  34. {
  35. System.setProperty("webdriver.gecko.driver", "./Drivers\\geckodriver.exe");
  36. driver = new FirefoxDriver();
  37. }
  38. else if(br.equals("ie"))
  39. {
  40. System.setProperty("webdriver.ie.driver", "./Drivers\\IEDriverServer.exe");
  41. driver = new InternetExplorerDriver();
  42. }
  43. driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
  44. driver.get("http://the-internet.herokuapp.com/");
  45. driver.manage().window().maximize();
  46. }
  47. @Test(dataProvider = "LoginData")
  48. public void loginTest(String user, String pwd, String exp)
  49. {
  50. driver.get("http://the-internet.herokuapp.com/");
  51. driver.findElement(By.xpath("//a[contains(text(),'FormAuthentication')]")).click();
  52. WebElementtxtEmail=driver.findElement(By.xpath("//input[@id='username']"));
  53. txtEmail.clear();
  54. txtEmail.sendKeys(user);
  55. logger.info("user name provided");
  56. WebElementtxtPassword=driver.findElement(By.xpath("//input[@id='password']"));
  57. txtPassword.clear();
  58. txtPassword.sendKeys(pwd);
  59. logger.info("passward provided");
  60. driver.findElement(By.xpath("//i[contains(text(),'Login')]")).click();//Login button
  61. String exp_title="The Internet";
  62. String act_title=driver.getTitle();
  63. System.out.println(act_title);
  64. WebDriverWait wait = new WebDriverWait(driver, 5);
  65. if(exp.equals("Valid"))
  66. {
  67. if(exp_title.equals(act_title))
  68. {
  69. logger.info("Login Passed");
  70. Assert.assertTrue(true);
  71. driver.findElement(By.xpath("//body/div[2]/div[1]/div[1]/a[1]")).click();
  72. }
  73. else
  74. {
  75. Assert.assertTrue(false);
  76. logger.info("Login failed");
  77. }
  78. }
  79. else if(exp.equals("Invalid"))
  80. {
  81. if(exp_title.equals(act_title))
  82. {
  83. Assert.assertTrue(false);
  84. logger.info("Login passed");
  85. driver.findElement(By.xpath("//body/div[2]/div[1]/div[1]/a[1]")).click();
  86. }
  87. else
  88. {
  89. Assert.assertTrue(true);
  90. logger.info("Login failed");
  91. }
  92. }
  93. }
  94. @DataProvider(name="LoginData")
  95. public String [][] getData() throws IOException
  96. {
  97. String loginData[][]= {
  98. {"tomsmith", "SuperSecretPassword!", "Valied"},
  99. {"tomsmith", "SuperSecretPasswor!", "Invalied"},
  100. {"tomsmit", "SuperSecretPassword!", "Invalied"},
  101. {"tomsmith", "SuperSecretPassword!", "Valied"},
  102. };
  103. return loginData;
  104. }
  105. @AfterClass
  106. public void tearDown()
  107. {
  108. driver.quit();
  109. }
  110. }

我的TestNG套件

  1. <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
  2. <suite name="LoginPageTemplateV2">
  3. <listeners>
  4. <listener class-name="com.LoginPage.Utilities.Reporting" />
  5. </listeners>
  6. <test name="LoginPage Test">
  7. <parameter name="browser" value="firefox" />
  8. <classes>
  9. <!-- <class name="com.inetbanking.testCases.TC_LoginTest_001" />-->
  10. <!-- class name="com.inetbanking.testCases.TC_LoginDDT_002" />-->
  11. <class name="com.LoginPage.Testcase.LoginPageDDT" />
  12. </classes>
  13. </test>
  14. </suite>

我的测试日志

  1. 2022-03-26 11:35:17,071 - LoginPage -INFO - user name provided
  2. 2022-03-26 11:35:17,656 - LoginPage -INFO - passward provided
  3. 2022-03-26 11:35:19,742 - LoginPage -INFO - user name provided
  4. 2022-03-26 11:35:19,987 - LoginPage -INFO - passward provided
  5. 2022-03-26 11:35:21,978 - LoginPage -INFO - user name provided
  6. 2022-03-26 11:35:22,193 - LoginPage -INFO - passward provided
  7. 2022-03-26 11:35:23,874 - LoginPage -INFO - user name provided
  8. 2022-03-26 11:35:24,045 - LoginPage -INFO - passward provided

("user name provided", "passward provided")是在“If else”条件之外编写脚本的日志; ("Login Passed", "Login Failed")是在“If else”条件**内编写脚本的日志,**不是日志记录。我尝试更改不同版本的Log4j依赖项。但没有任何效果。

zysjyyx4

zysjyyx41#

您似乎在getData()方法中犯了一个打字错误,这就是为什么代码没有进入if else块的原因。请替换下面的代码,然后重试。

  1. I am Facing issue with Log4j. When I run my below Test case, it was fine with output and all tests has been passed. But Log4j was not Logging properly. It was logging data only from outside of "If else" condition and skipping data from inside "If else" condition.
  2. My Test script:
  3. package com.LoginPage.Testcase;
  4. import java.io.IOException;
  5. import java.util.concurrent.TimeUnit;
  6. import org.apache.log4j.Logger;
  7. import org.apache.log4j.PropertyConfigurator;
  8. import org.openqa.selenium.By;
  9. import org.openqa.selenium.WebDriver;
  10. import org.openqa.selenium.WebElement;
  11. import org.openqa.selenium.chrome.ChromeDriver;
  12. import org.openqa.selenium.firefox.FirefoxDriver;
  13. import org.openqa.selenium.ie.InternetExplorerDriver;
  14. import org.openqa.selenium.support.ui.WebDriverWait;
  15. import org.testng.Assert;
  16. import org.testng.annotations.AfterClass;
  17. import org.testng.annotations.BeforeClass;
  18. import org.testng.annotations.DataProvider;
  19. import org.testng.annotations.Parameters;
  20. import org.testng.annotations.Test;
  21. public class LoginPageDDT {
  22. public static WebDriver driver;
  23. public static Logger logger;
  24. @Parameters("browser")
  25. @BeforeClass
  26. public void setup(String br)
  27. {
  28. logger = Logger.getLogger("LoginPage");
  29. PropertyConfigurator.configure("Log4j.properties");
  30. if(br.equals("chrome"))
  31. {
  32. System.setProperty("webdriver.chrome.driver", "./Drivers\\chromedriver.exe");
  33. driver=new ChromeDriver();
  34. }
  35. else if(br.equals("firefox"))
  36. {
  37. System.setProperty("webdriver.gecko.driver", "./Drivers\\geckodriver.exe");
  38. driver = new FirefoxDriver();
  39. }
  40. else if(br.equals("ie"))
  41. {
  42. System.setProperty("webdriver.ie.driver", "./Drivers\\IEDriverServer.exe");
  43. driver = new InternetExplorerDriver();
  44. }
  45. driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
  46. driver.get("http://the-internet.herokuapp.com/");
  47. driver.manage().window().maximize();
  48. }
  49. @Test(dataProvider = "LoginData")
  50. public void loginTest(String user, String pwd, String exp)
  51. {
  52. driver.get("http://the-internet.herokuapp.com/");
  53. driver.findElement(By.xpath("//a[contains(text(),'FormAuthentication')]")).click();
  54. WebElementtxtEmail=driver.findElement(By.xpath("//input[@id='username']"));
  55. txtEmail.clear();
  56. txtEmail.sendKeys(user);
  57. logger.info("user name provided");
  58. WebElementtxtPassword=driver.findElement(By.xpath("//input[@id='password']"));
  59. txtPassword.clear();
  60. txtPassword.sendKeys(pwd);
  61. logger.info("passward provided");
  62. driver.findElement(By.xpath("//i[contains(text(),'Login')]")).click();//Login button
  63. String exp_title="The Internet";
  64. String act_title=driver.getTitle();
  65. System.out.println(act_title);
  66. WebDriverWait wait = new WebDriverWait(driver, 5);
  67. if(exp.equals("Valid"))
  68. {
  69. if(exp_title.equals(act_title))
  70. {
  71. logger.info("Login Passed");
  72. Assert.assertTrue(true);
  73. driver.findElement(By.xpath("//body/div[2]/div[1]/div[1]/a[1]")).click();
  74. }
  75. else
  76. {
  77. Assert.assertTrue(false);
  78. logger.info("Login failed");
  79. }
  80. }
  81. else if(exp.equals("Invalid"))
  82. {
  83. if(exp_title.equals(act_title))
  84. {
  85. Assert.assertTrue(false);
  86. logger.info("Login passed");
  87. driver.findElement(By.xpath("//body/div[2]/div[1]/div[1]/a[1]")).click();
  88. }
  89. else
  90. {
  91. Assert.assertTrue(true);
  92. logger.info("Login failed");
  93. }
  94. }
  95. }
  96. @DataProvider(name="LoginData")
  97. public String [][] getData() throws IOException
  98. {
  99. String loginData[][]= {
  100. {"tomsmith", "SuperSecretPassword!", "Valid"},
  101. {"tomsmith", "SuperSecretPasswor!", "Invalid"},
  102. {"tomsmit", "SuperSecretPassword!", "Invalid"},
  103. {"tomsmith", "SuperSecretPassword!", "Valid"},
  104. };
  105. return loginData;
  106. }
展开查看全部

相关问题