English 中文(简体)
I m 在对物体进行 h取时出现错误,因此,自动化文字正在失效。
原标题:I m getting an error while hovering over the object and hence the automation script is failing

我已经为日志一页写了一个简单的基于 Sel的自动化文字。 但是,我有一个单独的页面物体和方法类别,另一个可变部件类别,然后是测试类别。

Page objects and methods

public class LoginPage extends ReusableComponents {
    WebDriver driver;
    
    @FindBy(xpath="//input[@name= username ]")
    public WebElement in_username;
    
    @FindBy(xpath="//input[@name= password ]")
    WebElement in_password;
    
    @FindBy(xpath="//button[@type= submit ]")
    WebElement btn_submit;
    
    public LoginPage(WebDriver driver) {
        super(driver);
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }
    
    public void enterUname(String uname)  {
        in_username.sendKeys(uname);
    }
    
    public void enterPassword(String pass) {
        in_password.sendKeys(pass);
    }
    
    public void clickSubmit() {
        btn_submit.click();
    }   
}

Reusable component class

public class ReusableComponents {
    
    WebDriver driver;

    public ReusableComponents(WebDriver driver) {
        this.driver = driver;
    }
    
    public void setup()  {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
    }

    public void waitElementToAppear(WebElement findby){
        
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.visibilityOfElementLocated((By) findby));
                
    }
    
    public void closeBrowser() {
        driver.close();
    }   
}

Test class

public class OpenBrowser {
    static WebDriver driver;

    public static void main(String[] args) {
        
        LoginPage login = new LoginPage(driver);
        ReusableComponents common = new ReusableComponents(driver);
        common.setup();
        common.waitElementToAppear(login.in_username);
        login.enterUname("Admin");
        login.enterPassword("admin123");
        login.clickSubmit();
        common.closeBrowser();
        
    }
}

Error Log

    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052}) on port 31559
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Jun 29, 2023 8:43:53 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Exception in thread "main" java.lang.ClassCastException: class jdk.proxy2.$Proxy4 cannot be cast to class org.openqa.selenium.By (jdk.proxy2.$Proxy4 is in module jdk.proxy2 of loader  app ; org.openqa.selenium.By is in the unnamed module of loader  app )
    at com.orange.reusablecomponents.ReusableComponents.waitElementToAppear(ReusableComponents.java:32)
    at com.orangehrm.OpenBrowser.main(OpenBrowser.java:27)

如果我对“用户名称”标的置换式的测试,那么它给我留下的错误就象我在此附上的那样。 请有人帮助我解决这个问题?

错误的筛选:

“Screenshot

我试图制造一个网点,但却没有帮助。

问题回答

在您的情形下,当你试图投下一个字标:jdk.proxy2.$Proxy<4/code>至org. openqa.selenium时,便会产生这一问题。 通过,这不是有效的投放作业。

在使用“Factory”时,它为附加说明的“FindBy”变量创造了近似值。 职业卫生员负责寻找要素等任务。 例如,当使用司机时,使用限定用途时,它会与代理人进行互动,然后使用实际方法。 因此,这个班子是按班次分类的。

在你的具体情况下,你实际上试图投下一个近似的物体:org. openqa.selenium.remote.RemoteWebElementorg. openqa.selenium。 不允许通过

通过对你的法典作以下修改,应当解决这一例外情况:

wait.until(ExpectedConditions.visibilityOf(webelement));

请参看下文为我制订的法典。 还需要在启动“URL”网站之后再加一等。

// TODO Auto-generated method stub
    WebDriverManager.chromedriver().setup();
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();        
    driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
    Thread.sleep(3000);
    driver.findElement(By.name("username")).sendKeys("Admin");
    driver.findElement(By.name("password")).sendKeys("admin123");
    driver.findElement(By.xpath("//button[@type= submit ]")).click();
    

In the Page objects and methods class you have used:

PageFactory.initElements(driver, this);

https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/PageFactory.html“rel=“nofollow noreferer”>。 班次

  • initElements​(SearchContext searchContext, java.lang.Class<T> pageClassToProxy): This method instantiates an instance of the given class, and set a lazy proxy for each of the WebElement and List<WebElement> fields that have been declared, assuming that the field name is also the HTML element s "id" or "name". This method returns an instantiated instance of the class with WebElement and List<WebElement> fields proxied.

因此,@FindBys:

  • @FindBy(xpath="//input[@name= username ]") public WebElement in_username;
  • @FindBy(xpath="//input[@name= password ]") WebElement in_password;
  • @FindBy(xpath="//button[@type= submit ]") WebElement btn_submit;

are proxied fileds. When you try to use those in conjunction with By class:

wait.until(ExpectedConditions.visibilityOfElementLocated((By) findby));

阁下:

Exception in thread "main" java.lang.ClassCastException: class jdk.proxy2.$Proxy4 cannot be cast to class org.openqa.selenium.By (jdk.proxy2.$Proxy4 is in module jdk.proxy2 of loader  app ; org.openqa.selenium.By is in unnamed module of loader  app )

Solution

为等到<><>>>> 用户名<>/code>>> 外地的可见度,并发送一个特性序列,可在<><>上添加两种方法。 日 本 类别如下:

public void LoginPage()
{
    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(LoginPage.getUsernameElement()));
    email.sendKeys("TestBite");
}

public WebElement getUsernameElement()
{
    return in_username;
}

References

你可以在以下几个方面找到相关的详细讨论:

您不明确等待有关内容,而是可以在您的网页上添加一个默认的等待时间。

// Set the timeout value (in seconds) as per your needs
PageFactory.initElements(new AjaxElementLocatorFactory(driver, 10), this); 




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...