Monday, September 16, 2024

Using Code to Verify Web Page Accessibility

 To check if a page is available on a particular domain, you can use Java's HttpURLConnection class to send a HTTP request to the page's URL and check the response code. Here's a simple example:


import java.net.HttpURLConnection;

import java.net.URL;


public class Main {

    public static void main(String[] args) {

        try {

            URL url = new URL("http://www.example.com");

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("GET");

            connection.connect();


            int responseCode = connection.getResponseCode();

            if (responseCode == 200) {

                System.out.println("Page is available");

            } else {

                System.out.println("Page is not available. Response code: " + responseCode);

            }

        } catch (Exception e) {

            System.out.println("Error: " + e.getMessage());

        }

    }

}

Replace "http://www.example.com" with the URL of the page you want to check. This code sends a GET request to the URL and checks if the response code is 200 (HTTP OK), which indicates that the page is available. If the response code is anything other than 200, it means the page is not available.


We can also automate the process of opening multiple URLs in Chrome using Puppeteer, a Node.js library that provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default but can be configured to run full (non-headless) Chrome or Chromium.


const puppeteer = require('puppeteer');


async function openUrls() {

    const browser = await puppeteer.launch();

    const page = await browser.newPage();


    const urls = ['http://www.example.com', 'http://www.example2.com', 'http://www.example3.com'];


    for (let url of urls) {

        await page.goto(url);

        await page.waitForTimeout(5000); // wait for 5 seconds before going to the next URL

    }


    await browser.close();

}


openUrls();


Code to execute this using AJAX

var urls = ['http://www.example1.com', 'http://www.example2.com', 'http://www.example3.com'];


urls.forEach(function(url) {

    $.ajax({

        url: url,

        type: 'GET',

        success: function(res) {

            console.log('Successfully fetched data from ' + url);

        },

        error: function(err) {

            console.log('Error fetching data from ' + url);

        }

    });

});


fetch('https://www.example.com')

    .then(response => {

        console.log('Response status:', response.status);

    })

    .catch(error => console.log('Error:', error));


In JavaScript, you can iterate over multiple URLs using a loop. Here's an example using a for...of loop and the fetch API to send a request to each URL:

const urls = ['http://example1.com', 'http://example2.com', 'http://example3.com'];


for (const url of urls) {

    fetch(url)

        .then(response => console.log(`Status of ${url}: ${response.status}`))

        .catch(error => console.log(`Error fetching ${url}: ${error}`));

}

No comments:

Post a Comment