@Bailrod
Thank you for sharing the MHTML file. That makes any studies much easier.
I have made a GitHub repository to propose my solution for you, see this:
You can down load the zip of the project at https://github.com/kazurayam/benef_grid/releases
Test Cases/TC1 shows my proposed solution. When I execute TC1, it emitted following output in the console this would tell you how it ran:
***************
supprimerARow(driver, pattern=1er) was invoked
2021-02-04 11:10:49.450 INFO c.k.k.c.keyword.builtin.CommentKeyword - Number of rows: 3
cell(0,1)=ROLLAND, matchFound=false
cell(0,2)=RONY, matchFound=false
cell(0,3)=174 RUE DU LIEUTENANT MOISANT , matchFound=false
cell(0,4)=77190 , matchFound=false
cell(0,5)= , matchFound=false
cell(0,6)= , matchFound=false
cell(0,7)=Non réglé , matchFound=false
cell(0,8)=2nd, matchFound=false
cell(1,1)=DURAND, matchFound=false
cell(1,2)=CLARENCE, matchFound=false
cell(1,3)=CRIFO-MME DENION / BP 31528 37 BIS QUAI DE VERSAILLES BP, matchFound=false
cell(1,4)=44015 , matchFound=false
cell(1,5)= , matchFound=false
cell(1,6)= , matchFound=false
cell(1,7)=Non réglé , matchFound=false
cell(1,8)=2nd, matchFound=false
cell(2,1)=ADAM, matchFound=false
cell(2,2)=, matchFound=false
cell(2,3)=LUSTIG SARL 1 RUE AUGUSTE HIMLY, matchFound=false
cell(2,4)=67000 , matchFound=false
cell(2,5)=03 88 02 70 92 , matchFound=false
cell(2,6)=03 88 80 92 20 , matchFound=false
cell(2,7)=Non réglé , matchFound=false
cell(2,8)=1er, matchFound=true
Yah! I clicked the button to removed this row ------ the row is should be removed by JavaScript
Test Cases/TC0 is the mimic of your original code using the MHTML file as input.
What is the difference between the TC0 and TC1?
Your original code TC0 wants to do everything at once in a loop. This design is not good; easily broken, hard to read. You should design your code with the ancient paradigm Divide-and-conquer in mind.
TC1 introduces an internal function named supprimerARow()
which tries to click the Supprimer
button at most 1 time. And the TC1 script body calls the function multiple times until it finds all rows have been appropriately processed.
import java.nio.file.Path
import java.nio.file.Paths
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
int loopCount = 0
int MAX_REPEAT = 3 // exit infinite loop if the loopCount execeded the max
Path projectDir = Paths.get(RunConfiguration.getProjectDir())
Path mhtml = projectDir.resolve('benef_grid.mhtml')
String url = mhtml.toFile().toURI().toURL().toExternalForm()
WebUI.openBrowser('')
WebUI.navigateToUrl(url)
WebUI.delay(1)
WebDriver driver = DriverFactory.getWebDriver()
String expectedValue = '1er'
Boolean removed = supprimerARow(driver, expectedValue); loopCount += 1
while (removed && loopCount <= MAX_REPEAT) {
removed = supprimerARow(driver, expectedValue); loopCount += 1
}
WebUI.closeBrowser()
/**
* try to find a row with text containing the expected value, and "click" amd "remove" it.
* if you want to remove multiple rows in the table, call this function mutile times until
* it returns 0.
*
* @param driver instance of WebDriver
* @param String the expected value. e.g. '2nd'
* @return true if a row was found and "clicked"; false in case no row was found
*/
Boolean supprimerARow(WebDriver driver, String pattern) {
println "***************\n supprimerARow(driver, pattern=${pattern}) was inveked"
'the conteneur table'
WebElement benef_grid = driver.findElement(By.xpath('//div[@class="conteneur"]/table/tbody'))
'capture all the rows available in the table '
List<WebElement> rows = benef_grid.findElements(By.tagName('tr'))
WebUI.comment("Number of rows: ${rows.size()}")
if (rows.size() > 0) {
for (int rx = 0; rx < rows.size(); rx++) {
List<WebElement> cols = rows[rx].findElements(By.tagName('td'))
Boolean matchFound = false
for (int cx = 1; cx < cols.size(); cx++) {
if (cols[cx].getText().equalsIgnoreCase(pattern)) {
matchFound = true
}
println "cell(${rx},${cx})=${cols[cx].getText()}, matchFound=${matchFound}"
if (matchFound) {
break
}
}
'this row contains a text matching with the given pattern'
if (matchFound) {
'click the check box of the row'
cols[0].click()
println "Yah! I clicked the button to removed this row ------ the row is should be removed by JavaScript"
'ensure the page gets stable'
WebUI.delay(1)
'1 row was found and processed'
return true
} else {
; 'continue to process the following row'
}
}
'We found no row which contains text matching with the given pattern'
return false
} else {
'zero row found'
return false
}
}
Note: the initial version of TC1 script went down into an infinite loop. In order for the code to exit from the infinite loop, I introduced a magical constant MAX_REPEAT = 3
. @Bailrod, you can change this to MAX_REPEAT = 100
or something.
Why infinite loop? Because the web page reproduced from the MHTML file worked wrongly. The web page DOES NOT respond to UI interactions at all. Test Case fails to click the radiobutton. The “Supprimer” button does not respond when clicked.
Why the web page reproduced from MHTML works wrongly? ---- it is because Chrome browser exclude all javascript fragments when it saves the page into a MHTML file. Therefore the reproduced web pages looks similar to the original but is dumb; does not interact at all.