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
action.moveToElement(tc2.getWomanLink()).build().perform();
is working absolutely fine.