I want to loop throw a text file that stores a URL links the problem is that every time i run the test the browser opens the API links one after another and closes them one by one, and only on the last one did the test completely and pass. there is a way to fix this issue?
there is my code:
@Test public void JeneratorTest() throws Throwable { System.out.println("starting itineraryJeneratorTest"); // crate driver System.setProperty("webdriver.chrome.driver", "src\main\resources\chromedriver.exe"); WebDriver driver = new ChromeDriver(); // maximize browser window driver.manage().window().maximize(); // open web page by looping on a text file Thread.sleep(2000); try { BufferedReader reader = new BufferedReader(new FileReader("url/Untitled 1")); String line; while ((line = reader.readLine()) != null) { driver.get(line); Thread.sleep(500); } } catch (IOException e) { e.printStackTrace(); } driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS); // click agree to the terms WebElement agrreToTerms = driver.findElement(By.xpath("/html//div[@id='root']/div[1]//button]")); agrreToTerms.click();
Advertisement
Answer
Its because you are performing actions outside of the loop
. Call those actions inside the loop
.
while ((line = reader.readLine()) != null) { driver = new ChromeDriver(); driver.get(line); Thread.sleep(500); WebElement agrreToTerms = driver.findElement(By.xpath("/html//div[@id='root']/div[1]//button]")); agrreToTerms.click(); driver.close(); }
It’s always better to use Explicit
wait instead of Implicit
wait or Thread.sleep()
.