SKV
SKV

Reputation: 133

Selenium moveToElement() to read the tool tip is not working

After adding an invalid file type to file upload section a validation message will be displayed as a ToolTip, I am not able to validate the ToolTip even after using moveToElement();

Here is the code that I used to make the tooltip visible

`driver.findElement(By.xpath(""//input[@type='file']"").sendKeys(invalidLicenseFileType.txt);
Actions actions = new Actions(driver);
actions.moveToElement(//*[contains(@class, 'fieldlabel-content')]).build().perform();`

ToolTip validation message image

HTML Code:

<div class="fieldlabel-content">
    <label class="fileinput fileinput--invalid">
    <div class="fileinput-cell fileinput-cell-input">
    <input placeholder="Choose a .license file..." accept=".license" value="" tabindex="0" type="file">
    <div class="fileinput-description">
    File:
    <span class="fileinput-description-file">
    <span class="icon fa fa-file-text-o"></span>
    invalidLicenseFileType.txt
    </span>
    </div>
    </div>
    <div class = "popup popup--theme-error popup--position-top popup--hover" style="top: 131px; left: 399px;">Validation message for uploading invalid file type.</div>
    </label>

Upvotes: 1

Views: 2462

Answers (4)

SKV
SKV

Reputation: 133

Implementing the following code help fixing my issue.

 try 
 {
     String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover',true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
     ((JavascriptExecutor) driver).executeScript(mouseOverScript,uploadFileInputFieldWebElement);
     Thread.sleep(1000);
     invalidLicenseFileTypeMessag = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;",tooltipWebElement);


} catch (Exception e) {
    exception message;

}

Upvotes: 1

Monika
Monika

Reputation: 732

I can see some mistakes in your code.

Code:

driver.findElement(By.xpath("//input[@type='file']")).sendKeys("invalidLicenseFileType.txt");
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.xpath("//*[contains(@class,'fieldlabel-content')]"));
actions.moveToElement(element).build().perform();
String toolTipText = driver.findElement(By.xpath("//div[contains(@class,'popup popup--theme-error')]")).getText();
System.out.println("Tool tip text present :- " + toolTipText);

// Compare tool tip text
Assert.assertEquals("Validation message for uploading invalid file type.", toolTipText);

Upvotes: 0

Anuj Teotia
Anuj Teotia

Reputation: 1323

I have two suggestions to solve this problem :

First : Using JavaScript

WebElement element = driver.findElement(By.xpath("value of xpath")).sendKeys(invalidLicenseFileType.txt);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].onmouseover()", element);

In the last line, in place of element , you can give the xpath of your WebElement over which you want to hover.

Second : Using Actions

Try using clickAndHold in place of moveToElement in Actions class.

WebElement element = driver.findElement(By.xpath("value of xpath")).sendKeys(invalidLicenseFileType.txt);
Actions actions = new Actions(driver);
actions.clickAndHold(//*[contains(@class, 'fieldlabel-content')]).build().perform();

Upvotes: 1

Buaban
Buaban

Reputation: 5137

I assume that the problem is the action try to hover mouse on the element, but the element is not displayed yet. Can you try code below

driver.findElement(By.xpath("//input[@type='file']").sendKeys("invalidLicenseFileType.txt");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions. presenceOfElementLocated(By.cssSelector(".popup--theme-error")));
Actions actions = new Actions(driver);
actions.moveToElement("//*[contains(@class, 'fileinput-description-file')]").perform();

Upvotes: 0

Related Questions