How to switch to window title "if it exists"

I’ve been searching for some answers but didn’t find anything about that, so if it already have been asked, I’m sorry.

Here is how my test works :

  • open browser to MyURL
  • enter a code
  • a new window is opened
    and my test can continue.

In the case where the code is invalid, the software don’t react.
So I would like to manage the case where there is or not a window index with value = 2

Here is my script if it can help :slight_smile:

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.annotation.Keyword as Keyword
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.exception.StepErrorException as StepErrorException
import as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.Keys as Keys
import org.openqa.selenium.WebElement as WebElement
import org.openqa.selenium.By.ByXPath as ByXPath
import org.junit.After
import org.openqa.selenium.By as By
import as Windows
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject

 CustomKeywords.'bdd.mysql.ConnexionBaseLocal'(GlobalVariable.url_base, GlobalVariable.port_base, GlobalVariable.db_name_base, 
    GlobalVariable.user_base, GlobalVariable.password_base)

 CustomKeywords.'bdd.mysql.execute'('delete from _0000000_produit where prod_code_barre ilike \'_ALTA_%\'')

 CustomKeywords.'bdd.mysql.execute'('delete from _0000000_lot where lot_produit_code_barre ilike \'_ALTA_%\'')

 CustomKeywords.'openbrowser.DemarrageFirefox.AvecSynchro'()'PageEncaissement/button_MENU'))'PageMenu0/BoutonMenu4_Produits'))'PageMenu4_Produits/BoutonProGestion Produits'))


 errors = []

 for (def LigneNum = 1; LigneNum <= findTestData('MajPrixTabac').getRowNumbers(); LigneNum++){'PageMenu4_Produits/Page_Menu_Gestion Produits/button_Code Manuel'))

WebUI.setText(findTestObject('PageMenu4_Produits/Page_Menu_GestionProduits/Page_GestProd_CodeManuel/input_ClavierVisuel'), findTestData('MajPrixTabac').getValue(1, LigneNum))'PageMenu4_Produits/Page_Menu_GestionProduits/Page_GestProd_CodeManuel/button_Code Altadis'))

 WebUI.waitForElementClickable(findTestObject('PageMenu4_Produits/Page_Menu_GestionProduits/Page_Fiche Produit/button_Tous ses fournisseurs'), 0)

 input_ttc = findTestObject('Object Repository/PageMenu4_Produits/Page_Menu_GestionProduits/Page_Fiche Produit/input_Prix TTC_fp_prod_prix')

 WebUI.verifyElementAttributeValue(input_ttc, 'value', findTestData('MajPrixTabac').getValue(2, LigneNum), 0, FailureHandling.CONTINUE_ON_FAILURE)'PageMenu4_Produits/Page_Menu_Gestion Produits/Page_FicheProduit/button_Valider'))


Hope I’ve been clear enough.
Thanks for your help.

I think we might need to see the error(s) you are receiving.

(I also edited your post to make your code look nicer - edit it yourself to see how I did that).

Thank you @Russ_Thomas for your answer (and my post look nicer indeed :wink: )
The thing is that actually my test works fine if the code entered at this line, is correct :

WebUI.setText(findTestObject('PageMenu4_Produits/Page_Menu_GestionProduits/Page_GestProd_CodeManuel/input_ClavierVisuel'), findTestData('MajPrixTabac').getValue(1, LigneNum))

But if not, I would get en error saying : Unable to switch to window with index: ‘2’
(Sorry i actually cannot give the complete error, but if needed i could tomorrow)

I tried different things but, being not skilled in coding it wasn’t successful. I hope someone could have an idea for the best way to do that.

Please post your error (as complete as possible).

The only way to fail switchWindow is if the index 2 does not exist. It’s hard to see why setText could have an effect so, yes, please post the error.

Triple backticks to the rescue!

2019-12-27 09:16:12.335 ERROR c.k.k.core.keyword.internal.KeywordMain  - ❌ Cannot find window with index: '2' (Root cause: com.kms.katalon.core.exception.StepFailedException: Cannot find window with index: '2'

This is the error i get, but it is normal because the window index 2 doesn’t open if the code is wrong.
I can manage the case where the code is correct because the window index 2 is open. In the case where the code is wrong and the index 2 doesn’t open give a failure.
I would like the test detect that the code is wrong, give a log if possible, and keep running the loop to check the other codes.

Nobody have an answer or an idea? :disappointed_relieved: