0

I have automated some test cases mentioned at https://www.techlistic.com/2019/08/automate-buy-product-using-selenium.html For second test case mentioned at given link, I have written code for mouse hover like this:

driver.get("http://automationpractice.com/index.php");
        
WebElement womanLink = driver.findElement(By.xpath("//*[@id=\"block_top_menu\"]/ul/li[1]/a"));
Actions action = new Actions(driver);
action.moveToElement(womanLink).build().perform();
        
TestCase2 tc2 = new TestCase2(driver);
tc2.clickTShirtsLink();

This piece of code is working absolutely fine but since I have used Page Object Model I don't know how can I return WebElement womanLink = driver.findElement(By.xpath("//*[@id=\"block_top_menu\"]/ul/li[1]/a")); from page object class.
I have tried this:

//page object class code
@FindBy(how = How.XPATH, using = "//*[@id=\\\"block_top_menu\\\"]/ul/li[1]/a")
WebElement womanLink;

public WebElement getWomanLink() {
        return womanLink;
    }
//test class code
driver.get("http://automationpractice.com/index.php");
TestCase2 tc2 = new TestCase2(driver);
Actions action = new Actions(driver);
action.moveToElement(tc2.getWomanLink()).build().perform();

but it is giving error: org.openqa.selenium.json.JsonException: java.lang.reflect.InvocationTargetException
I have explored Google and YouTube, on YouTube I have found some videos of mouse hover but they are without page object model approach.
Found two similar problems on stackover flow, links:
https://stackoverflow.com/questions/57202599/how-to-use-mouse-hover-action-in-page-object-model-class-java
https://stackoverflow.com/questions/54050524/how-should-i-use-webelements-and-actions-through-page-object-model
but these problems didn't help to modify the code.
Since I have used Page object model approach, WebElement womanLink path must be present in page object class. Have tried multiple techniques but not getting how can I achieve this?
Note: If above code snippets are not enough, here is my GitHub repository link https://github.com/atif-dev/EcommerceBuyProduct-Automation- for whole code

4
  • Can you share full stack trace? – Alexey R. Jun 7 at 14:41
  • I'm guessing it's because of your xpath value "//*[@id=\\\"block_top_menu\\\"]/ul/li[1]/a" . Try to use "//*[@id='block_top_menu']/ul/li[1]/a" – Millie Anne Volante Jun 8 at 4:45
  • give your class , test code , including full stack trace as alexey mentioned – PDHide Jun 8 at 16:52
  • @MillieAnneVolante Thanks a lot. Yes you are right, my xpath value was wrong. I have changed the xpath value in page object class. Now action.moveToElement(tc2.getWomanLink()).build().perform(); is working absolutely fine. – Atif Iqbal Jun 9 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.