【问题标题】:Can you tell me where i did mistake你能告诉我我在哪里做错了吗
【发布时间】:2020-08-13 09:16:59
【问题描述】:
package hrmInfoModule;
       import java.util.concurrent.TimeUnit;
       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.interactions.Actions;    import org.testng.annotations.AfterTest;    import org.testng.annotations.BeforeTest;    import org.testng.annotations.Test;
       public class Hrmlogin {
        
    public WebDriver driver;
    
    @BeforeTest
    public void openbrowser()
    {
        System.setProperty("webdriver.chrome.driver", "D:\\Software\\chromedriver.exe");
        driver =  new ChromeDriver ();
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    }
    
    @Test
    public void login() throws InterruptedException
    {
        String Url="https://opensource-demo.orangehrmlive.com/";
        String ID ="Admin";
        String Pswrd="admin123";
        driver.get(Url);
        WebElement login = driver.findElement(By.id("txtUsername"));
        login.sendKeys(ID);
        WebElement paswrd = driver.findElement(By.id("txtPassword"));
        paswrd.sendKeys(Pswrd);
        WebElement btn = driver.findElement(By.id("btnLogin"));
        btn.click();
        Thread.sleep(2000);
    }
    
    @Test
    public WebElement Admin() throws InterruptedException
    {
        Thread.sleep(2000);
        WebElement Admin = driver.findElement(By.xpath(("//*[@id='menu_admin_viewAdminModule']")));
        return Admin;
    }
    
    @Test
    public WebElement JobTitle(WebElement Admin) throws InterruptedException
    {
        Thread.sleep(2000);
        WebElement job = driver.findElement(By.xpath("//*[@id='menu_admin_viewAdminModule']//following-sibling::ul/li[2]/a"));
        WebElement jobtitles = driver.findElement(By.xpath("//*[@id='menu_admin_viewAdminModule']//following-sibling::ul/li[2]/ul/li[1]/a"));
        Actions act= new Actions(driver); 
        Thread.sleep(2000);
        act.moveToElement(Admin).moveToElement(job).moveToElement(jobtitles).click().build().perform();
        WebElement AddBtn= driver.findElement(By.xpath(("//input[@value='Add']")));
        Thread.sleep(2000);
        AddBtn.click();
        driver.findElement(By.xpath(("//*[@id='jobTitle_jobTitle']"))).sendKeys("Manager");
        driver.findElement(By.xpath(("//*[@id='jobTitle_jobDescription']"))).sendKeys("zxc");
        driver.findElement(By.xpath(("//*[@id='jobTitle_note']"))).sendKeys("jobTitle_note");
        WebElement SaveBtn= driver.findElement(By.xpath(("//input[@value='Save']")));
        Thread.sleep(2000);
        SaveBtn.click();
        return job;
    }
    
    @Test
    public void PayGrade(WebElement job) throws InterruptedException
    {
        //WebElement job = driver.findElement(By.xpath("//*[@id='menu_admin_viewAdminModule']//following-sibling::ul/li[2]/a"));
        WebElement payGrade = driver.findElement(By.xpath("//*[@id='menu_admin_viewAdminModule']//following-sibling::ul/li[2]/ul/li[2]/a"));
        Actions act= new Actions(driver); 
        act.moveToElement(job).moveToElement(payGrade).click().build().perform();
        WebElement AddPayBtn= driver.findElement(By.xpath(("//input[@value='Add']")));
        Thread.sleep(2000);
        AddPayBtn.click();
        driver.findElement(By.xpath(("//*[@id='payGrade_name']"))).sendKeys("INR");
        WebElement SavePayBtn= driver.findElement(By.xpath(("//input[@value='Save']")));
        Thread.sleep(2000);
        SavePayBtn.click();
    }
    
    @AfterTest
    public void teardown()
    {
        driver.quit();
    }    }

在上面的代码中,TestNG 只通过了一个名为 Login() 的测试。谁能告诉我为什么会这样? 如何改进此代码并运行一系列测试。

【问题讨论】:

  • 因为你在login()测试中打开了url。在@AfterTest 方法中浏览器被关闭。浏览器在Admin() 测试之前启动,但 URL 从未打开
  • 所以.. abhishek agarwal 可以通过简单地关闭 login() 测试来测试您是否正确 Fenio?

标签: java selenium selenium-webdriver testng


【解决方案1】:

当我运行你的代码时,我得到以下日志:

PASSED: login
FAILED: PayGrade
org.testng.TestNGException: 
Cannot inject @Test annotated Method [PayGrade] with [interface org.openqa.selenium.WebElement].
For more information on native dependency injection please refer to https://testng.org/doc/documentation-main.html#native-dependency-injection

我认为将WebElement job 作为PayGrade(WebElement job) 的参数是不对的,所以尝试修复这种代码。

编辑 1:

代码cmets中的详细信息:

package hrmInfoModule;

import java.util.concurrent.TimeUnit;
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.interactions.Actions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Hrmlogin {

    public WebDriver driver;
    
    /*
     * put common xpath string here
     */
    String xJob = "//a[@id='menu_admin_Job']";
    String xJobTitles = "//a[@id='menu_admin_viewJobTitleList']";
    String xPayGrade = "//a[@id='menu_admin_viewPayGrades']";

    @BeforeTest
    public void openbrowser() {
        System.setProperty("webdriver.chrome.driver", "D:\\Software\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    }

    @Test (priority = 0)
    public void testLogin() throws InterruptedException {
        String Url = "https://opensource-demo.orangehrmlive.com/";
        String ID = "Admin";
        String Pswrd = "admin123";
        driver.get(Url);
        WebElement login = driver.findElement(By.id("txtUsername"));
        login.sendKeys(ID);
        WebElement paswrd = driver.findElement(By.id("txtPassword"));
        paswrd.sendKeys(Pswrd);
        WebElement btn = driver.findElement(By.id("btnLogin"));
        btn.click();
        Thread.sleep(2000);
    }
    
    @Test (priority = 1)
    public void testAdmin() throws InterruptedException {
        WebElement admin = driver.findElement(By.xpath(("//*[@id='menu_admin_viewAdminModule']")));
        /**
         * Notice:
         * 1. If there are no effective steps in this function, it should be removed.
         * 2. click() is more stable than Actions.moveTo()
         */
        admin.click();
        Thread.sleep(2000);
    }

    @Test (priority = 2)
    public void testJobTitle() throws InterruptedException {
        Thread.sleep(2000);
        WebElement job = driver.findElement(By.xpath(xJob));
        WebElement jobTitles = driver.findElement(By.xpath(xJobTitles));
        Actions act = new Actions(driver);
        Thread.sleep(2000);
        
        act.moveToElement(job).moveToElement(jobTitles).click().build().perform();
        
        WebElement AddBtn = driver.findElement(By.xpath(("//input[@value='Add']")));
        Thread.sleep(2000);
        AddBtn.click();
        driver.findElement(By.xpath(("//*[@id='jobTitle_jobTitle']"))).sendKeys("Manager");
        driver.findElement(By.xpath(("//*[@id='jobTitle_jobDescription']"))).sendKeys("zxc");
        driver.findElement(By.xpath(("//*[@id='jobTitle_note']"))).sendKeys("jobTitle_note");
        WebElement SaveBtn = driver.findElement(By.xpath(("//input[@value='Save']")));
        SaveBtn.click();
        Thread.sleep(2000);
    }

    @Test (priority = 3)
    public void testPayGrade() throws InterruptedException {
        WebElement job = driver.findElement(By.xpath(xJob));
        WebElement payGrade = driver.findElement(By.xpath(xPayGrade));
        Actions act = new Actions(driver);
        act.moveToElement(job).moveToElement(payGrade).click().build().perform();
        WebElement AddPayBtn = driver.findElement(By.xpath(("//input[@value='Add']")));
        Thread.sleep(2000);
        AddPayBtn.click();
        driver.findElement(By.xpath(("//*[@id='payGrade_name']"))).sendKeys("INR");
        WebElement SavePayBtn = driver.findElement(By.xpath(("//input[@value='Save']")));
        Thread.sleep(2000);
        SavePayBtn.click();
    }

    @AfterTest
    public void teardown() {
        driver.quit();
    }
}

【讨论】:

  • 感谢您的建议..但是我如何在不同的 TestCase(如 JobTitle 和 PayGrade)中调用 WebElements 'Admin' 和 'Job'。我是否应该根据其一次又一次地定位元素的管理员和工作类型用法?
  • @abhishek agarwal 请用不同的@test 方法重新定位元素。该建议基于两个因素:1)测试方法隔离互不影响2)页面操作可能会导致DOM更改,重新定位会更安全。此外,也许你需要了解更多TestNG basics
  • WebElement 的层次结构是 Admin---->Job-----> 职位也与薪酬等级平行。所以我需要重新定位元素,即管理员然后工作,然后只有我才能找到职位和薪酬等级。根据给定的建议,如果我在不同的 2 测试方法中重新定位元素,那么它将成功运行职位,但支付等级测试用例将不会成功运行。因此,我需要传递 web 元素。希望你能收到我的消息
  • @abhishekagarwal 我已经修改了答案。希望对您有用,如果是,请单击此答案前面的有用按钮:)
  • 感谢您的不断回答。您的回答部分有用,但我的问题仍然与“testJobTitles”和“testPayGrade”方法相同,您正在为 WebElement“Job”重写代码。所以我在想有什么办法可以克服这种重写。