How to use getUrl method of com.qaprosoft.carina.core.foundation.report.ReportContext class

Best Carina code snippet using com.qaprosoft.carina.core.foundation.report.ReportContext.getUrl

Source:ReportContext.java Github

copy

Full Screen

...231 public static List<String> listArtifacts(WebDriver driver) {232 List<String> artifactNames = Arrays.stream(Objects.requireNonNull(getArtifactsFolder().listFiles()))233 .map(File::getName)234 .collect(Collectors.toList());235 String hostUrl = getUrl(driver, "");236 String username = getField(hostUrl, 1);237 String password = getField(hostUrl, 2);238 239 try {240 HttpURLConnection con = (HttpURLConnection) new URL(hostUrl).openConnection();241 con.setInstanceFollowRedirects(true); //explicitly define as true because default value doesn't work and return 301 status242 con.setRequestMethod("GET");243 if (!username.isEmpty() && !password.isEmpty()) {244 String usernameColonPassword = username + ":" + password;245 String basicAuthPayload = "Basic " + Base64.getEncoder().encodeToString(usernameColonPassword.getBytes());246 con.addRequestProperty("Authorization", basicAuthPayload);247 }248 int responseCode = con.getResponseCode();249 String responseBody = readStream(con.getInputStream());250 if (responseCode == HttpURLConnection.HTTP_NOT_FOUND &&251 responseBody.contains("\"error\":\"invalid session id\",\"message\":\"unknown session")) {252 throw new RuntimeException("Invalid session id. Something wrong with driver");253 }254 if (responseCode == HttpURLConnection.HTTP_OK) {255 String hrefAttributePattern = "href=([\"'])((?:(?!\\1)[^\\\\]|(?:\\\\\\\\)*\\\\[^\\\\])*)\\1";256 Pattern pattern = Pattern.compile(hrefAttributePattern);257 Matcher matcher = pattern.matcher(responseBody);258 while (matcher.find()) {259 if (!artifactNames.contains(matcher.group(2))) {260 artifactNames.add(matcher.group(2));261 }262 }263 }264 } catch (IOException e) {265 LOGGER.debug("Something went wrong when try to get artifacts from remote", e);266 } 267 return artifactNames;268 }269 270 /**271 * Get artifacts from auto download folder of local or remove driver session by pattern272 * 273 * @param driver WebDriver274 * @param pattern String - regex for artifacts 275 * @return list of artifact files276 */277 public static List<File> getArtifacts(WebDriver driver, String pattern) {278 List<String> filteredFilesNames = listArtifacts(driver)279 .stream()280 // ignore directories281 .filter(fileName -> !fileName.endsWith("/"))282 .filter(fileName -> fileName.matches(pattern))283 .collect(Collectors.toList());284 List<File> artifacts = new ArrayList<>();285 for (String fileName : filteredFilesNames) {286 artifacts287 .add(getArtifact(driver, fileName));288 }289 return artifacts;290 } 291 /**292 * Get artifact from auto download folder of local or remove driver session by name293 * 294 * @param driver WebDriver295 * @param name String - filename with extension296 * @return artifact File297 */298 public static File getArtifact(WebDriver driver, String name) {299 File file = new File(getArtifactsFolder() + File.separator + name);300 if (file.exists()) {301 return file;302 }303 304 String path = file.getAbsolutePath();305 LOGGER.debug("artifact file to download: " + path);306 String url = getUrl(driver, name);307 String username = getField(url, 1);308 String password = getField(url, 2);309 310 if (!username.isEmpty() && !password.isEmpty()) {311 Authenticator.setDefault(new CustomAuthenticator(username, password));312 } 313 if (checkArtifactUsingHttp(url, username, password)) {314 try {315 FileUtils.copyURLToFile(new URL(url), file);316 LOGGER.debug("Successfully downloaded artifact: {}", name);317 } catch (IOException e) {318 LOGGER.error("Artifact: " + url + " wasn't downloaded to " + path, e);319 }320 } else {321 Assert.fail("Unable to find artifact: " + name);322 }323 // publish as test artifact to Zebrunner Reporting324 Artifact.attachToTest(name, file); 325 return file;326 }327 /**328 * check if artifact exists using http329 * 330 * @param url String331 * @param username String332 * @param password String333 * @return boolean334 */335 private static boolean checkArtifactUsingHttp(String url, String username, String password) {336 try {337 HttpURLConnection.setFollowRedirects(false);338 // note : you may also need339 // HttpURLConnection.setInstanceFollowRedirects(false)340 HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();341 con.setRequestMethod("HEAD");342 if (!username.isEmpty() && !password.isEmpty()) {343 String usernameColonPassword = username + ":" + password;344 String basicAuthPayload = "Basic " + Base64.getEncoder().encodeToString(usernameColonPassword.getBytes());345 con.addRequestProperty("Authorization", basicAuthPayload);346 }347 return (con.getResponseCode() == HttpURLConnection.HTTP_OK);348 } catch (Exception e) {349 LOGGER.debug("Artifact doesn't exist: " + url, e);350 return false;351 }352 }353 /**354 * get username or password from url355 * 356 * @param url String357 * @param position int358 * @return String359 */360 private static String getField(String url, int position) {361 Pattern pattern = Pattern.compile(".*:\\/\\/(.*):(.*)@");362 Matcher matcher = pattern.matcher(url);363 return matcher.find() ? matcher.group(position) : "";364 }365 366 /**367 * Generate file in artifacts location and register in Zebrunner Reporting368 * 369 * @param name String370 * @param source InputStream371 */ 372 public static void saveArtifact(String name, InputStream source) throws IOException {373 File artifact = new File(String.format("%s/%s", getArtifactsFolder(), name));374 artifact.createNewFile();375 FileUtils.writeByteArrayToFile(artifact, IOUtils.toByteArray(source));376 377 Artifact.attachToTest(name, IOUtils.toByteArray(source));378 }379 /**380 * Copy file into artifacts location and register in Zebrunner Reporting381 * @param source File382 */ 383 public static void saveArtifact(File source) throws IOException {384 File artifact = new File(String.format("%s/%s", getArtifactsFolder(), source.getName()));385 artifact.createNewFile();386 FileUtils.copyFile(source, artifact);387 388 Artifact.attachToTest(source.getName(), artifact);389 } 390 /**391 * generate url for artifact by name392 * 393 * @param driver WebDriver394 * @param name String395 * @return String396 */397 private static String getUrl(WebDriver driver, String name) {398 String seleniumHost = Configuration.getSeleniumUrl().replace("wd/hub", "download/");399 WebDriver drv = (driver instanceof EventFiringWebDriver) ? ((EventFiringWebDriver) driver).getWrappedDriver() : driver;400 String sessionId = ((RemoteWebDriver) drv).getSessionId().toString();401 String url = seleniumHost + sessionId + "/" + name;402 LOGGER.debug("url: " + url);403 return url;404 }405 private static void stopThreadLogAppender() {406 try {407 LoggerContext loggerContext = (LoggerContext) LogManager.getContext(true);408 ThreadLogAppender appender = loggerContext.getConfiguration().getAppender("ThreadLogAppender");;409 if (appender != null) {410 appender.stop();411 }...

Full Screen

Full Screen

getUrl

Using AI Code Generation

copy

Full Screen

1import static com.qaprosoft.carina.core.foundation.report.ReportContext.*;2import static com.qaprosoft.carina.core.foundation.report.ReportContext.*;3import static com.qaprosoft.carina.core.foundation.report.ReportContext.*;4import org.testng.annotations.Test;5import com.qaprosoft.carina.core.foundation.report.ReportContext;6public class ReportContextTest {7public void testReportContext() {8String url = getUrl();9String screenshot = getScreenshot();10String screenshot = getScreenshot();11}12}13import static com.qaprosoft.carina.core.foundation.report.ReportContext.*;14String url = getUrl();15import static com.qaprosoft.carina.core.foundation.report.ReportContext.*;16String screenshot = getScreenshot();

Full Screen

Full Screen

getUrl

Using AI Code Generation

copy

Full Screen

1String url = ReportContext.getUrl();2String screenshot = ReportContext.getScreenshot();3String video = ReportContext.getVideo();4String log = ReportContext.getLog();5String reportFolder = ReportContext.getReportFolder();6String suiteName = ReportContext.getSuiteName();7String testName = ReportContext.getTestName();8String testDescription = ReportContext.getTestDescription();9String testMessage = ReportContext.getTestMessage();10String testMessage = ReportContext.getTestMessage();11String testMessage = ReportContext.getTestMessage();12String testMessage = ReportContext.getTestMessage();13String testMessage = ReportContext.getTestMessage();14String testMessage = ReportContext.getTestMessage();15String testMessage = ReportContext.getTestMessage();16String testMessage = ReportContext.getTestMessage();

Full Screen

Full Screen

getUrl

Using AI Code Generation

copy

Full Screen

1String url = ReportContext.getUrl("path to file");2String screenshot = ReportContext.getScreenshot("path to file");3String video = ReportContext.getVideo("path to file");4String attachment = ReportContext.getAttachment("path to file");5String report = ReportContext.getReport("path to file");6String log = ReportContext.getLog("path to file");7String reportDir = ReportContext.getReportDir("path to file");8String reportDir = ReportContext.getReportDir("path to file", "report name");9String reportDir = ReportContext.getReportDir("path to file", "report name", "report version");10String reportDir = ReportContext.getReportDir("path to file", "report name", "report version", "report environment");11String reportDir = ReportContext.getReportDir("path to file", "report name", "report version", "report environment", "report build");12String reportDir = ReportContext.getReportDir("path to file", "report name", "report version", "report environment", "report build", "report platform");13String reportDir = ReportContext.getReportDir("path to file", "report name", "report version", "report environment

Full Screen

Full Screen

getUrl

Using AI Code Generation

copy

Full Screen

1String url = ReportContext.getUrl();2String dir = ReportContext.getReportDir();3String dir = ReportContext.getReportDir();4String dir = ReportContext.getReportDir();5String dir = ReportContext.getReportDir();6String dir = ReportContext.getReportDir();7String dir = ReportContext.getReportDir();8String dir = ReportContext.getReportDir();9String dir = ReportContext.getReportDir();

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful