0

I have to use multiple URLs in same script in Selenium (Java) and have to visit one at a time, perform some operations on the page take a screenshot at the end, and then move one to the next page and repeat the same steps. Is this task doable with Selenium? I know it can be done through loops otherwise but can I do it with Selenium?

P.S. I'm a beginner in Selenium and I have searched the internet thoroughly before posting the question without finding a solution.

1
  • What do you mean under doing with loop VS doing with selenium? You can combine power of loops with features of selenium. Can you add some example you have.. Like how you iterate over 1 url.
    – Alexey R.
    Jun 3, 2021 at 14:07

3 Answers 3

0

If we sum up the OPs question as:

Can I use Selenium to test multiple URLs?

Then the answer is: Yes.

Like with most questions in Selenium, the answer, in part, is using Page Objects (POMs). I'd setup any repeatable code in a Page Object that you then use in your test files.

An example with the OPs question, I'd setup a navigation class that loads each URL; separate classes with page elements and methods that represent the different pages you navigate to; a utilities method that handles screenshots. So far, none of this is using loops.

With the test files, I'd have one test file for each URL. Use a beforeAll to open the URL that calls your navigation class; have one or many test methods that perform some operation; have an afterTest which takes a screenshot after each test. Again, repeat this for as many URLs you need to test. This then allows the test runner to run all the tests. Again, no loops are needed.

If you don't want or need separate test files for each URL, then you can handle URL reload on each test method.

0
0

Yes, this is achievable through Selenium. Given that selenium is an automation tool, and you can write your script logic in languages like Java or Python there is nothing stopping you from writing a script mentioned above, respective to software testing service.

A straight forward logic would be,

(pseudo code)

test(Webdriver driver){
List<String> urlList = getUrls();
for(String url : urlLists){
    driver.get(url);
    performTask(driver);
    takeScreenshot(driver);
}

}

A more test-centric approach would be to pass url as test data, so test would run n times for n urls.

test(Webdriver driver, String url){    
driver.get(url);
performTask(driver);
takeScreenshot(driver);}

You can further refactor using Page Object Model as per your specific use case with respect to your application.

0
0

Try using DataProvider(), you can loop all urls and check result with same method,

here is snippet of code -

package com.termcjAutomation.testCases;

import java.io.IOException;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;



public class TC_021_AllJourneyPreQuoteElementVerify extends BaseClass{
    

    @Test(enabled=true,dataProvider = "PreQuotePageElements")
    @Description("Verify PreQuotePageElements")
    @Severity(SeverityLevel.CRITICAL)
    public void verifyPrequotePageElements(String journeyType, String journeyElementLocator, String baseURL, String expectedHeaderDisclaimer) throws InterruptedException {
       
  
        
        driver.get(baseURL);

        Thread.sleep(4000);
        
        if (baseURL.equalsIgnoreCase(driver.getCurrentUrl())){
            
            Assert.assertTrue(true, "baseUrl is correct : " + baseURL);
            logger.info("baseUrl is correct : " + baseURL );
        } 
        else
        {
            logger.info("baseURL is incorrect :" + driver.getCurrentUrl());
            Assert.assertTrue(false, "baseURL is incorrect : " + driver.getCurrentUrl());
        }
       
        if (expectedHeaderDisclaimer.equalsIgnoreCase(preQuotePage.getDisclaimerPrequoteHeaderText())) {
            Assert.assertTrue(true, "sucess text : " + expectedHeaderDisclaimer);
            logger.info("logger text : " + expectedHeaderDisclaimer );
        }
        else 
        {
            logger.info("print text :" + preQuotePage.getDisclaimerPrequoteHeaderText());
            Assert.assertTrue(false, "failure text : " +preQuotePage.getDisclaimerPrequoteHeaderText());
        }
    }
        



@DataProvider(name = "PreQuotePageElements")
public Object[][] PreQuotePageElements() throws IOException {
    String path = System.getProperty("user.dir") + "yoursheetpath.xlsx";

    int rownum = XLUtils.getRowCount(path, "Sheet1");
    int colcount = XLUtils.getCellCount(path, "Sheet1", 1);

    Object[][] validationdata = new Object[rownum][colcount];

    for (int i = 1; i <= rownum; i++) {
        for (int j = 0; j < colcount; j++) {
            validationdata[i - 1][j] = XLUtils.getCellData(path, "Sheet1", i, j);
        }
    }
    return validationdata;
}
}

  • Make sure you have installed testng in your selenium java project
  • Provide correct path of your file where urls and others data is stored

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.