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

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

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

package com.LoginPage.Testcase;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class LoginPageDDT {

    public static WebDriver driver;

    public static Logger logger;

    @Parameters("browser")
    @BeforeClass

    public void setup(String br)
    {
        logger = Logger.getLogger("LoginPage");
        PropertyConfigurator.configure("Log4j.properties");
        if(br.equals("chrome"))
        {
            System.setProperty("webdriver.chrome.driver", "./Drivers\\chromedriver.exe");  
            driver=new ChromeDriver();
        }
        else if(br.equals("firefox"))
        {
            System.setProperty("webdriver.gecko.driver", "./Drivers\\geckodriver.exe");  
            driver = new FirefoxDriver();
        }
        else if(br.equals("ie"))
        {
            System.setProperty("webdriver.ie.driver", "./Drivers\\IEDriverServer.exe");       
            driver = new InternetExplorerDriver();
        }

        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        driver.get("http://the-internet.herokuapp.com/");
        driver.manage().window().maximize();
    }

    @Test(dataProvider = "LoginData")

    public void loginTest(String user, String pwd, String exp)
    {
                driver.get("http://the-internet.herokuapp.com/");

                    driver.findElement(By.xpath("//a[contains(text(),'FormAuthentication')]")).click();

                WebElementtxtEmail=driver.findElement(By.xpath("//input[@id='username']"));
                txtEmail.clear();
                txtEmail.sendKeys(user);
                logger.info("user name provided");

                WebElementtxtPassword=driver.findElement(By.xpath("//input[@id='password']"));
                txtPassword.clear();
                txtPassword.sendKeys(pwd);
                logger.info("passward provided");

                driver.findElement(By.xpath("//i[contains(text(),'Login')]")).click();//Login  button

                String exp_title="The Internet";
                String act_title=driver.getTitle();
                System.out.println(act_title);
                WebDriverWait wait = new WebDriverWait(driver, 5);

                if(exp.equals("Valid"))
                {
                    if(exp_title.equals(act_title))
                    {
                        logger.info("Login Passed");
                        Assert.assertTrue(true);

                        driver.findElement(By.xpath("//body/div[2]/div[1]/div[1]/a[1]")).click();

                    }
                    else
                    {
                        Assert.assertTrue(false);
                        logger.info("Login failed");
                    }
                }
                else if(exp.equals("Invalid"))
                {
                    if(exp_title.equals(act_title))
                    {

                        Assert.assertTrue(false);
                        logger.info("Login passed");

                        driver.findElement(By.xpath("//body/div[2]/div[1]/div[1]/a[1]")).click();

                    }
                    else
                    {
                        Assert.assertTrue(true);
                        logger.info("Login failed");
                    }
                }
    }

    @DataProvider(name="LoginData")
    public String [][] getData() throws IOException
    {
        String loginData[][]= {

                {"tomsmith", "SuperSecretPassword!", "Valied"},
                {"tomsmith", "SuperSecretPasswor!", "Invalied"},
                {"tomsmit", "SuperSecretPassword!", "Invalied"},
                {"tomsmith", "SuperSecretPassword!", "Valied"},
                            };

        return loginData;
        }

    @AfterClass
    public void tearDown()
    {
        driver.quit();
    }

}

我的TestNG套件

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="LoginPageTemplateV2">

    <listeners>
        <listener class-name="com.LoginPage.Utilities.Reporting" />
    </listeners>

    <test name="LoginPage Test">
        <parameter name="browser" value="firefox" />
        <classes>
            <!-- <class name="com.inetbanking.testCases.TC_LoginTest_001" />-->
             <!-- class name="com.inetbanking.testCases.TC_LoginDDT_002" />-->
            <class name="com.LoginPage.Testcase.LoginPageDDT" />

        </classes>

    </test>

</suite>

我的测试日志

2022-03-26 11:35:17,071 - LoginPage -INFO - user name provided
2022-03-26 11:35:17,656 - LoginPage -INFO - passward provided
2022-03-26 11:35:19,742 - LoginPage -INFO - user name provided
2022-03-26 11:35:19,987 - LoginPage -INFO - passward provided
2022-03-26 11:35:21,978 - LoginPage -INFO - user name provided
2022-03-26 11:35:22,193 - LoginPage -INFO - passward provided
2022-03-26 11:35:23,874 - LoginPage -INFO - user name provided
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块的原因。请替换下面的代码,然后重试。

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.

My Test script:

package com.LoginPage.Testcase;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class LoginPageDDT {

    public static WebDriver driver;

    public static Logger logger;

    @Parameters("browser")
    @BeforeClass

    public void setup(String br)
    {
        logger = Logger.getLogger("LoginPage");
        PropertyConfigurator.configure("Log4j.properties");
        if(br.equals("chrome"))
        {
            System.setProperty("webdriver.chrome.driver", "./Drivers\\chromedriver.exe");  
            driver=new ChromeDriver();
        }
        else if(br.equals("firefox"))
        {
            System.setProperty("webdriver.gecko.driver", "./Drivers\\geckodriver.exe");  
            driver = new FirefoxDriver();
        }
        else if(br.equals("ie"))
        {
            System.setProperty("webdriver.ie.driver", "./Drivers\\IEDriverServer.exe");       
            driver = new InternetExplorerDriver();
        }

        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        driver.get("http://the-internet.herokuapp.com/");
        driver.manage().window().maximize();
    }

    @Test(dataProvider = "LoginData")

    public void loginTest(String user, String pwd, String exp)
    {
                driver.get("http://the-internet.herokuapp.com/");

                    driver.findElement(By.xpath("//a[contains(text(),'FormAuthentication')]")).click();

                WebElementtxtEmail=driver.findElement(By.xpath("//input[@id='username']"));
                txtEmail.clear();
                txtEmail.sendKeys(user);
                logger.info("user name provided");

                WebElementtxtPassword=driver.findElement(By.xpath("//input[@id='password']"));
                txtPassword.clear();
                txtPassword.sendKeys(pwd);
                logger.info("passward provided");

                driver.findElement(By.xpath("//i[contains(text(),'Login')]")).click();//Login  button

                String exp_title="The Internet";
                String act_title=driver.getTitle();
                System.out.println(act_title);
                WebDriverWait wait = new WebDriverWait(driver, 5);

                if(exp.equals("Valid"))
                {
                    if(exp_title.equals(act_title))
                    {
                        logger.info("Login Passed");
                        Assert.assertTrue(true);

                        driver.findElement(By.xpath("//body/div[2]/div[1]/div[1]/a[1]")).click();

                    }
                    else
                    {
                        Assert.assertTrue(false);
                        logger.info("Login failed");
                    }
                }
                else if(exp.equals("Invalid"))
                {
                    if(exp_title.equals(act_title))
                    {

                        Assert.assertTrue(false);
                        logger.info("Login passed");

                        driver.findElement(By.xpath("//body/div[2]/div[1]/div[1]/a[1]")).click();

                    }
                    else
                    {
                        Assert.assertTrue(true);
                        logger.info("Login failed");
                    }
                }
    }

    @DataProvider(name="LoginData")
    public String [][] getData() throws IOException
    {
        String loginData[][]= {

                {"tomsmith", "SuperSecretPassword!", "Valid"},
                {"tomsmith", "SuperSecretPasswor!", "Invalid"},
                {"tomsmit", "SuperSecretPassword!", "Invalid"},
                {"tomsmith", "SuperSecretPassword!", "Valid"},
                            };

        return loginData;
        }

相关问题