vinsr04
vinsr04

Reputation: 39

Selenium MoveToElement does not work but test cases are passing

In Below scenario, I am trying to select Option4-1 from the main menu. Have used moveToElement(), click(). use case shows as pass when i execute my script but i don't see the slide in window appearing which is expected behavior after clicking "option4-1".

Image shows option menus

Code:

public class CreateppPage extends PageFactory {

private WebDriver driver;
private WebDriverWait wait;
private Actions act;
/**
      Selectors section
 */

@FindBy(xpath = "//div[@class='Main Button']")
private WebElement AddMenu;
@FindBy(xpath="//li[@class='item-submenu']//span[contains(text(),'Option4')]")
private WebElement subMenu;
@FindBy(xpath="//span[contains(text(),'Option4-1')]")
private WebElement subsubMenu;

/*****************Methods section***********************/

public CreateppPage(WebDriver driver, long wait) {
    
    this.driver = driver;
    this.wait = new WebDriverWait(driver,wait);
    initElements(driver, this);
    driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
}

public void createMpp() {
    
    wait.until(ExpectedConditions.visibilityOf(AddMenu));
    act = new Actions(driver);
    try {
        Thread.sleep(2000);
        act.moveToElement(AddMenu).click().build();
        Thread.sleep(1000);
        act.moveToElement(subMenu).click().moveToElement(subsubMenu).click().build();
        Thread.sleep(10000);    
    }
    catch(Exception e) {
        System.out.println(e.getCause());
    }
}
}

Upvotes: 1

Views: 2488

Answers (3)

YaDav MaNish
YaDav MaNish

Reputation: 1352

moveToElement() Moves the mouse to the middle of the element.

Build(): creates the chain of the actions that need to be 
performed

perform(): perform those chain of actions which is build by using 
Action build method.

Internally the perform() called the build() method so if we call 
the build().perform() explicitly so we are calling .build() twice

So it seems like you miss the .perform()

act.moveToElement(subMenu).click()
.moveToElement(subsubMenu).click().build().perform();

OR

act.moveToElement(subMenu).click()
.moveToElement(subsubMenu).click().perform();

.perform() : A convenience method for performing the actions without calling build() first.

Upvotes: 1

Prophet
Prophet

Reputation: 33361

You are missing .perform()
It should be

act.moveToElement(AddMenu).click().build().perform();
Thread.sleep(1000);
act.moveToElement(subMenu).moveToElement(subsubMenu).click().build().perform();
Thread.sleep(10000);    

I also suggest to reduce or even remove the hardcoded delays.
Explicit waits should be used instead.

wait.until(ExpectedConditions.visibilityOfElementLocated(AddMenu));
act.moveToElement(AddMenu).click().build().perform();
wait.until(ExpectedConditions.visibilityOfElementLocated(subsubMenu));
act.moveToElement(subMenu).moveToElement(subsubMenu).click().build().perform();

Upvotes: 0

cruisepandey
cruisepandey

Reputation: 29362

I would probably do like this :

new Actions(driver).moveToElement(AddMenu).click().moveToElement(subsubMenu).click().build().perform();

Upvotes: 1

Related Questions