Hi guys, today I will post a simple project that I did just to catch some information with Selenium and Java. So, I have a spreadsheet with some urls in the first column and when I run the project, it is being recorded the status code of the urls and how long is lasting to load this page in the following columns.
It is very simples. I know that there are many ways to do that and you don’t need to use selenium because will take more time to render the page and everything, but maybe someone is needing something similar. So, now the code and in the bottom the link to my project.
- OPEN URL – Open the URL and write the StatusCode and the time to load the page in the following columns:
import jxl.read.biff.BiffException; import org.openqa.selenium.WebDriver; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Workbook; import org.openqa.selenium.By; public class OpenUrl { OpenDataBase opendatabase = new OpenDataBase(); FileOutputStream fOut = null; public void OpenURL(WebDriver driver) throws BiffException, Exception { String tablename = ("tb"); opendatabase.Open(tablename); File file = new File("BD.xls"); int result = 0; String STR = "CRI -"; int ARQ = 1; int WARNING = 2; int CRITICAL = 3; Workbook wb = new HSSFWorkbook(new FileInputStream(file. getCanonicalPath())); for (int i = 0; i < opendatabase.sheet.getRows(); i++) { String url = opendatabase.sheet.getCell(0, i).getContents(); double start = System.currentTimeMillis(); driver.manage().window().maximize(); driver.get(url); double finish = System.currentTimeMillis(); double totalTime = finish - start; double totalTimeSec = totalTime / 1000; try { driver.findElement(By.xpath("/html/body/div/span[2]/ul/li/a") ).click(); } catch (Exception e) { driver.findElement(By.xpath("/html/body/div/span/ul/li/a") ).click(); } String titlepage = driver.getTitle().replace(" ", ""); FileWriter fw = new FileWriter(titlepage + ".txt"); int statusCode = Statuscode(driver.getCurrentUrl(), driver); String line = url + "," + totalTimeSec + "," + statusCode; fw.write(line); fw.close(); } } public int Statuscode(String URLName, WebDriver driver) { try { int statuscode = Integer.parseInt(driver.findElement(By.xpath ("/html/body/div/span/span/pre[2]")).getText().substring(9, 12)); return statuscode; } catch (Exception e) { e.printStackTrace(); return 1; } } }
- OPEN DATABASE CODE – Read the columns in the Excel Spreadsheet:
import java.io.File; import java.io.IOException; import jxl.Sheet; import jxl.Workbook; import jxl.WorkbookSettings; import jxl.read.biff.BiffException; public class OpenDataBase{ Sheet sheet; Workbook bdurl; public Sheet Open(String tablename) throws BiffException, IOException, Exception { File file = new File("BD.xls"); WorkbookSettings ws = new WorkbookSettings(); ws.setEncoding("Cp1252"); bdurl = Workbook.getWorkbook(new File(file.getCanonicalPath()), ws); sheet = bdurl.getSheet(tablename); return sheet; } public void Close(Workbook bdurl) throws BiffException, IOException, Exception { bdurl.close(); } }
- MAIN CLASS – statusCode:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import java.util.concurrent.TimeUnit; import org.openqa.selenium.firefox.internal.ProfilesIni; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; public class statusCode { public static void main(String[] args) throws Exception { ProfilesIni profilesIni = new ProfilesIni(); FirefoxProfile profile = profilesIni.getProfile("default"); WebDriver driver = new FirefoxDriver(profile); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); profile.setPreference("browser.ssl_override_behavior", 1); profile.setPreference("network.proxy.type", 0); profile.setAcceptUntrustedCertificates(true); profile.setAssumeUntrustedCertificateIssuer(false); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); OpenUrl Openurl = new OpenUrl(); Openurl.OpenURL(driver); driver.close(); driver.quit(); } }
Thanks guys ! See you next week, if you have any questions, suggestion please feel free to comment below.