你能告诉我我在哪里做错了吗

时间:2020-08-13 09:16:59

标签: java selenium selenium-webdriver testng

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()的测试。谁能告诉我为什么? 如何改进此代码并进行一系列测试。

1 个答案:

答案 0 :(得分:0)

运行代码时,会收到以下日志:

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:

代码注释中的详细信息:

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();
    }
}