0

I have scenario, in Excel file in first column having 3 URL. And with help of for loop i am able to launch first URL and trying to find whether single image is getting displayed or not. But in first attempt my assertion is getting failed and getting message as

": no such element: Unable to locate element: {"method".

so here my execution getting stopped. can anyone please suggest me how i can verify whether image is pass or fail for 3 URL . and how i can know for which page image is not present on page.

My code as below :-

package homework;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.concurrent.TimeUnit;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class NewTrialTranslationApiNew {
    WebDriver driver;

    @BeforeMethod
    public void setup() {

        System.setProperty("webdriver.chrome.driver",
                "C://Users//amittu//Downloads//chromedriver_win32//chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    }

    @Test
    public void testpages() throws Exception {

        File src1 = new File(
                "C://Users//amittu//Desktop//Translatelivelocales.xlsx");
        FileInputStream fis = new FileInputStream(src1);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
        XSSFSheet sheet1 = wb.getSheetAt(0);

        int rowcount = sheet1.getLastRowNum();

        System.out.println(rowcount);

        for (int i = 0; i <= rowcount; i++) {
            String URLofPage = sheet1.getRow(i).getCell(0).getStringCellValue();
            System.out.println("Value from excel with for loop is  " + URLofPage);

            driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

            driver.get(URLofPage);

            Boolean b = driver.findElement(
                    By.xpath("//img[@alt='Translation API']")).isDisplayed();
            SoftAssert softAssertion = new SoftAssert();
            softAssertion.assertTrue(b);
        }

    }

     @AfterMethod
     public void closebrowser(){
     driver.quit();
     }

}

1

Your test is failing. You should not alter the test so that it passes unless there is an obvious error with your code.

If this is not a real test, and just a utility to tell you what pages this image exists on, you could check for the existence of the image using findElements, then print the result for each website either at the point of checking, or at the end of the script.

public void example() {
    List<String> urls = Arrays.asList("url1", "url2", "url3");
    ArrayList<String> failures = new ArrayList<>();

    for(String url : urls) {
        driver.get(url);
        List<WebElement> images = driver.findElements(By.xpath("//img[@alt='Translation API']"));

        if (images.size() == 1 && images.get(0).isDisplayed()) {
                // Nothing, only interested in failures
        } else {
            failures.add(URLofPage);
        }
    }

    for(String failure : failures) {
        System.out.println("FAILED: " + failure);
    }

    assertEquals(0, failures.size());
}

The above is an example of storing up failures and then checking them at the end of the test. This is a simplified example that bypasses all the loading test data from excel. You'll still need that to do that as it's your test data source.

  • Thank you so much for your help – Amit Turkar Aug 30 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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