Click on a submenu element

Hello to everyone,
I want to click on a submenu element. Because the element is hide most of the time, I use a solution with mouseOver() and then clicking on the element. Is there a simplest way to do this task?
Secondly, I use this into a try-catch. Everything run perfectly but in my log, at the end of the test I have error. It looks weird to me, is it normal? I tried to forced it with markPassed() but without success…
In advance thanks to all
Regards

Code:

/**
 * help me to click on a hidden element of the menu 
 * 
 * @to TestObjet which we are looking to cick on
 * 
 * @lien id of the TestObject we want to click on
 * 
 * Void
 * 
 **/

def clickSubMenu(TestObject to, String lien) {
	String myPathParentMenu = "//a[contains(text(), '${lien}')]/parent::*/parent::*/parent::*[@class='submenu']/a"
	String myPathEnfantMenu = "//a[contains(text(), '${lien}')]/parent::*/parent::*/parent::*[@class='submenu']/ul/li[1]"
	TestObject monObjetTestParentMenu = new TestObject('')
	monObjetTestParentMenu.addProperty('xpath', ConditionType.EQUALS, myPathParentMenu)
	WebUI.mouseOver(monObjetTestParentMenu)
	TestObject monObjetTestEnfantMenu = new TestObject('')
	monObjetTestEnfantMenu.addProperty('xpath', ConditionType.EQUALS, myPathEnfantMenu)
	WebUI.mouseOver(monObjetTestEnfantMenu)
	WebUI.click(to)
}

//Ouvrir le navigateur
WebUI.openBrowser('https://www.planning-medical.com/accueil.php');

//L'ensemble des éléments à tester avec le titre que l'on doit trouver

def webPageMap = ['Accueil':'Accueil', 'offre':'L’offre en détail', 'détail':'L’offre en détail', 'Fonctionnalités':'Découvrez les fonctionnalités et les avantages de l\'application', 'Aperçu':'Copies d’écran de l’application Planning-Medical.comn', 'Témoignages':'Témoignages des utilisateurs', 'Collaboration':'Notre collaboration', 'Chronologie':'Notre collaboration', 'tarifs':'Les tarifs','contacter':'Nous contacter', 'Accès':'Identification']

//Pour le nombre d'éléments faire :
//	
//	Trouver le lien correspondant
//	Cliquer sur le lien
//	Verifier que le titre de la page correspond
//
//Fin pour

webPageMap.each {lien, titre ->
	
	//Trouve l'emplacement correspondant

	String myPath = "//a[contains(text(), '${lien}')]"
	WebUI.comment(myPath)
	TestObject monObjetTest = new TestObject('')
	monObjetTest.addProperty('xpath', ConditionType.EQUALS, myPath)
	WebUI.verifyElementPresent(monObjetTest, 1)
	
	try {
		//Clique sur lien correspondant
		WebUI.click(monObjetTest)
		//Clique sur les liens qui ne sont cachés dans des sous menus par exemple
	} catch(SetpFailedException) {
		
		clickSubMenu(monObjetTest, lien)
	
	}
	
}

//Fermer le navigateur
WebUI.closeBrowser()


Katalon is having difficulty accessing the A element with `href=“./offre.php”. It seems it is off screen when it tries to access it.

Hello @Russ_Thomas,
My method in first lines of my code is supposed to fix this issue. I first put the mouse on the element visible into the menu then put the mouse onto the submenu to keep it visible and finally, click on the element, which is now visible. Do you know if such a keyword already exist or not?
I am sure that it is working because I watch the test run on the browser and I can see all the pages loading one after an other. Even those accessible through a link hide into the submenu. I wonder if it is because of the try-catch structure. the catch is well execute to the end, I don’t understand why I can see an error at the end on the log. That is why I put a KeywordUtil.markPassed() to try to erase the error into the log.
I am not sure that my explanation are clear enough or if code or screen capture are missing, tell me if you need more to help you helping me :wink:

Instead of this:

WebUI.verifyElementPresent(monObjetTest, 1)

Try this:

WebUI.waitForElementVisible(monObjetTest, 10)

Hello @Russ_Thomas,
I’ve tried your solution at different positions with more or less results. For example, if I make a simple remplacement, the execution time rise to 80 seconds (20 before) and the log still send error message. I decided to put it into my method, after opening the submenu and it’s better, even though I still have error…
I’ve modified my script and added some comments to make it more understandable (I hope), here is where I am now:

/**
 * help me to click on a hidden element of the menu 
 * 
 * @to TestObjet which we are looking to cick on
 * 
 * @lien id of the TestObject we want to click on
 * 
 * Void
 * 
 **/
def clickSubMenu(TestObject to, String lien) {
	String myPathParentMenu = "//a[contains(text(), '${lien}')]/parent::*/parent::*/parent::*[@class='submenu']/a"
	String myPathEnfantMenu = "//a[contains(text(), '${lien}')]/parent::*/parent::*/parent::*[@class='submenu']/ul/li[1]"
	TestObject monObjetTestParentMenu = new TestObject("a_MenuParent_${lien}")
	monObjetTestParentMenu.addProperty('xpath', ConditionType.EQUALS, myPathParentMenu)
	WebUI.mouseOver(monObjetTestParentMenu)
	TestObject monObjetTestEnfantMenu = new TestObject("a_SubMenu_FirstElement")
	monObjetTestEnfantMenu.addProperty('xpath', ConditionType.EQUALS, myPathEnfantMenu)
	WebUI.mouseOver(monObjetTestEnfantMenu)
	WebUI.waitForElementVisible(to, 10)
	WebUI.click(to)
}

//Open the browser
WebUI.openBrowser('https://www.planning-medical.com/accueil.php');

//definition of all the element to test [id:title]

def webPageMap = ['Accueil':'Accueil', 'offre':'L’offre en détail', 'détail':'L’offre en détail', 'Fonctionnalités':'Découvrez les fonctionnalités et les avantages de l\'application', 'Aperçu':'Copies d’écran de l’application Planning-Medical.comn', 'Témoignages':'Témoignages des utilisateurs', 'Collaboration':'Notre collaboration', 'Chronologie':'Notre collaboration', 'tarifs':'Les tarifs','contacter':'Nous contacter', 'Accès':'Identification']

//For each element in the map :
//	
//	find the corresponding link in the DOM
//	Click on it
//	Verify with the title of the page that the right page is displayed
//
//end for each

webPageMap.each {lien, titre ->
	//Find the position of the link in the DOM
	
	WebUI.comment("Comment: lien -> ${lien}")

	String myPath = "//a[contains(text(), '${lien}')]"
	TestObject monObjetTest = new TestObject("a_${lien}")
	monObjetTest.addProperty('xpath', ConditionType.EQUALS, myPath)
	//WebUI.verifyElementPresent(monObjetTest, 1)
	//WebUI.waitForElementVisible(monObjetTest, 10)
	
	try {
		//Try to click on the element 
		WebUI.click(monObjetTest)
		WebUI.comment("Comment: try succeeded!")
	} catch(SetpFailedException) {
		WebUI.comment("Comment: an error have been catch")

		//Click on element hide into the submenu
		clickSubMenu(monObjetTest, lien)
	}
}

//Close the browser
WebUI.closeBrowser()


Anyway there is a lot of things that I don’t understand into the log window. I will show example:

  • First, I don’t understand why the error is expendable. It is not a conditional statement, why does it open?

  • Secondly I don’t understand why it is already relative to the next TestObject. I mean just the previous line it is a different TestObject and now it show an error to click on an object no one ask for…
    (at least not now)


In advance thanks :slight_smile:

It’s because you add WebUI.comment('Des messages') inside your function.

It is the same Caused By error (Element […] could not be scroll into view) ?

Hello @HeleneB

Yes it is. This error message is caused by the fact that the element is hide into the sub menu but I don’t understand because the message talk about the next TestObject (next in the point of view of the MAP defined at the beginning of the script) and not the one enter in the method’s parameters. It is the last step of the method and the line just before report the state of a test made on the right element, so why already talk about the next one?
Furthermore, it actually success to click on the button, I load the next page as a proof. More mysterious…

For now I am using the exact same way to click on an element of the submenu but not into a method. I supposed that doing it with a method is a proper way to code but if it give me some bad time… I don’t know…

I am also looking for other way to solve it, I have for example, find a way to completely replace the try-catch section by a if-else with a verifyElementPresent close of the solution gave by @Russ_Thomas

In advance merci beaucoup :smiley:

TO me, this implies that some other action is causing the click, not the one you’re expecting. But looking at your code, I cannot see it.

There is nothing wrong with using a method to encapsulate the purpose of

  1. creating a test object for a submenu
  2. Hovering over the submene
  3. Clicking a submenu item.

Either will work. But it should be said that…

  1. if and else provide selection of defined code paths under your control.
  2. try/catch may provide detours into code paths not under your control. For this reason, they’re typically used when something catastrophic occurs.

I hope you find the issue and look forward to your rsvp :wink: