Best Galen code snippet using com.galenframework.utils.GalenUtils.autoAdjustBrowserWindowSizeToFitViewport
Source:GalenUtils.java
...263 }264 }265 public static void resizeDriver(WebDriver driver, int width, int height) {266 if (GalenConfig.getConfig().getBooleanProperty(GalenProperty.GALEN_BROWSER_VIEWPORT_ADJUSTSIZE)) {267 GalenUtils.autoAdjustBrowserWindowSizeToFitViewport(driver, width, height);268 } else {269 driver.manage().window().setSize(new org.openqa.selenium.Dimension(width, height));270 }271 }272 public static File takeScreenshot(WebDriver driver) throws IOException {273 File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);274 if (GalenConfig.getConfig().shouldAutoresizeScreenshots()) {275 BufferedImage image = Rainbow4J.loadImage(file.getAbsolutePath());276 File newFile = File.createTempFile("screenshot", ".png");277 image = GalenUtils.resizeScreenshotIfNeeded(driver, image);278 Rainbow4J.saveImage(image, newFile);279 return newFile;280 }281 else return file;282 }283 284 public static Properties loadProperties(String fileName) throws IOException {285 286 GalenProperties properties = null;287 if (TestSession.current() != null) {288 properties = TestSession.current().getProperties();289 }290 else properties = new GalenProperties();291 292 properties.load(new File(fileName));293 return properties.getProperties();294 }295 296 public static void cookie(WebDriver driver, String cookie) {297 String script = "document.cookie=\"" + StringEscapeUtils.escapeJava(cookie) + "\";";298 injectJavascript(driver, script);299 }300 301 public static Object injectJavascript(WebDriver driver, String script) {302 return ((JavascriptExecutor)driver).executeScript(script);303 }304 305 public static Object[] listToArray(List<?> list) {306 if (list == null) {307 return new Object[]{};308 }309 Object[] arr = new Object[list.size()];310 return list.toArray(arr);311 }312 public static String getParentForFile(String filePath) {313 if (filePath != null) {314 return new File(filePath).getParent();315 }316 else return null;317 }318 public static InputStream findFileOrResourceAsStream(String filePath) {319 File file = new File(filePath);320 if (file.exists()) {321 try {322 return new FileInputStream(file);323 } catch (FileNotFoundException e) {324 throw new RuntimeException(e);325 }326 }327 else {328 if (!filePath.startsWith("/")) {329 filePath = "/" + filePath;330 }331 InputStream stream = GalenUtils.class.getResourceAsStream(filePath);332 if (stream != null) {333 return stream;334 }335 else {336 String windowsFilePath = filePath.replace("\\", "/");337 return GalenUtils.class.getResourceAsStream(windowsFilePath);338 }339 }340 }341 public static String calculateFileId(String fullPath) {342 InputStream is = GalenUtils.findFileOrResourceAsStream(fullPath);343 return calculateFileId(fullPath, is);344 }345 public static String calculateFileId(String fullPath, InputStream inputStream) {346 try {347 String fileName = new File(fullPath).getName();348 MessageDigest md = MessageDigest.getInstance("MD5");349 new DigestInputStream(inputStream, md);350 byte [] hashBytes = md.digest();351 return fileName + convertHashBytesToString(hashBytes);352 } catch (Exception ex) {353 throw new RuntimeException("Could not calculate file id", ex);354 }355 }356 private static String convertHashBytesToString(byte[] hashBytes) {357 StringBuilder builder = new StringBuilder();358 for (byte b : hashBytes) {359 builder.append(Integer.toHexString(0xFF & b));360 }361 return builder.toString();362 }363 public static Pattern convertObjectNameRegex(String regex) {364 String jRegex = regex.replace("#", "[0-9]+").replace("*", ".*");365 return Pattern.compile(jRegex);366 }367 public static boolean isObjectsSearchExpression(String singleExpression) {368 //check if it is group369 if (singleExpression.startsWith("&")) {370 return true;371 }372 for (int i = 0; i < singleExpression.length(); i++) {373 char symbol = singleExpression.charAt(i);374 if (symbol == '*' || symbol == '#') {375 return true;376 }377 }378 return false;379 }380 public static String toCommaSeparated(List<String> list) {381 if (list != null) {382 StringBuffer buff = new StringBuffer();383 boolean comma = false;384 for (String item : list) {385 if (comma) {386 buff.append(',');387 }388 comma = true;389 buff.append(item);390 }391 return buff.toString();392 }393 return "";394 }395 public static String removeNonPrintableControlSymbols(String line) {396 StringBuilder builder = new StringBuilder();397 char ch;398 for (int i = 0; i < line.length(); i++) {399 ch = line.charAt(i);400 if (ch >= 32 && ch < ZERO_WIDTH_SPACE_CHAR || ch == 9) {401 builder.append(ch);402 }403 }404 return builder.toString();405 }406 public static Dimension getViewportArea(WebDriver driver) {407 List<Number> size = (List<Number>)((JavascriptExecutor)driver).executeScript("return [document.documentElement.clientWidth" +408 "|| document.body.clientWidth" +409 "|| window.innerWidth," +410 "document.documentElement.clientHeight" +411 "|| document.body.clientHeight" +412 "|| window.innerHeight];"413 );414 return new Dimension(size.get(0).intValue(), size.get(1).intValue());415 }416 public static void autoAdjustBrowserWindowSizeToFitViewport(WebDriver driver, int width, int height) {417 driver.manage().window().setSize(new org.openqa.selenium.Dimension(width, height));418 Dimension viewport = getViewportArea(driver);419 if (viewport.getWidth() < width) {420 int delta = (int) (width - viewport.getWidth());421 driver.manage().window().setSize(new org.openqa.selenium.Dimension(width + delta, height));422 }423 }424 public static List<String> fromCommaSeparated(String parameters) {425 List<String> items = new LinkedList<>();426 String[] paramArray = parameters.split(",");427 for (String param : paramArray) {428 String trimmed = param.trim();429 if (!trimmed.isEmpty()) {430 items.add(trimmed);...
autoAdjustBrowserWindowSizeToFitViewport
Using AI Code Generation
1import com.galenframework.browser.Browser;2import com.galenframework.browser.BrowserFactory;3import com.galenframework.reports.GalenTestInfo;4import com.galenframework.reports.HtmlReportBuilder;5import com.galenframework.reports.model.LayoutReport;6import com.galenframework.specs.page.PageSpec;7import com.galenframework.utils.GalenUtils;8import org.openqa.selenium.WebDriver;9import org.testng.annotations.AfterMethod;10import org.testng.annotations.BeforeMethod;11import org.testng.annotations.Test;12import java.util.LinkedList;13import java.util.List;14import static com.galenframework.reports.model.LayoutReport.Status.ERROR;15import static com.galenframework.reports.model.LayoutReport.Status.PASSED;16import static java.util.Arrays.asList;17public class GalenTest {18 private static final String GALEN_REPORTS_PATH = "target/galen-reports";19 private WebDriver driver;20 private List<GalenTestInfo> tests;21 public void setUp() throws Exception {22 tests = new LinkedList<GalenTestInfo>();23 driver = BrowserFactory.getDriver(Browser.CHROME);24 }25 public void tearDown() throws Exception {26 GalenUtils.autoAdjustBrowserWindowSizeToFitViewport(driver, 1000);27 new HtmlReportBuilder().build(tests, GALEN_REPORTS_PATH);28 driver.quit();29 }30 public void testGalen() throws Exception {31 PageSpec pageSpec = new PageSpec();32 pageSpec.header(asList("header"));33 pageSpec.footer(asList("footer"));34 pageSpec.layout(asList("main"), asList("content"));35 LayoutReport layoutReport = GalenUtils.checkLayout(driver, pageSpec, asList("desktop"));36 GalenTestInfo test = GalenTestInfo.fromString("Galen test");37 test.getReport().layout(layoutReport, "Check layout");38 if (layoutReport.errors() > 0) {39 test.getReport().testError("Layout check failed", null);40 }41 tests.add(test);42 }43}
autoAdjustBrowserWindowSizeToFitViewport
Using AI Code Generation
1import com.galenframework.browser.Browser;2import com.galenframework.browser.BrowserFactory;3import com.galenframework.reports.GalenTestInfo;4import com.galenframework.reports.HtmlReportBuilder;5import com.galenframework.reports.model.LayoutReport;6import com.galenframework.specs.page.Locator;7import com.galenframework.specs.page.PageSpec;8import com.galenframework.specs.page.PageSpecReader;9import com.galenframework.specs.page.SectionFilter;10import com.galenframework.utils.GalenUtils;11import org.openqa.selenium.Dimension;12import org.openqa.selenium.WebDriver;13import org.openqa.selenium.chrome.ChromeDriver;14import org.openqa.selenium.chrome.ChromeOptions;15import org.openqa.selenium.firefox.FirefoxDriver;16import org.openqa.selenium.firefox.FirefoxProfile;17import org.openqa.selenium.ie.InternetExplorerDriver;18import org.openqa.selenium.remote.DesiredCapabilities;19import org.openqa.selenium.remote.RemoteWebDriver;20import org.testng.annotations.AfterMethod;21import org.testng.annotations.BeforeMethod;22import org.testng.annotations.DataProvider;23import org.testng.annotations.Test;24import java.io.IOException;25import java.net.URL;26import java.util.LinkedList;27import java.util.List;28import static java.util.Arrays.asList;29public class GalenTest {30 private WebDriver driver;31 private String baseUrl;32 private String gridHubUrl;33 private String browser;34 private String size;35 public void prepare() throws IOException {36 browser = "chrome";37 size = "1024x768";38 driver = createDriver(gridHubUrl, browser, size);39 driver.get(baseUrl);40 }41 public void teardown() {42 driver.quit();43 }44 @Test(dataProvider = "devices")45 public void testLayout(String deviceName, String size) throws IOException {46 load(baseUrl, size);47 checkLayout("/specs/example.spec", deviceName);48 }49 public Object[][] devices() {50 return new Object[][]{51 new Object[]{"mobile", "320x480"},52 new Object[]{"tablet", "600x800"},53 new Object[]{"desktop", "1024x768"}54 };55 }56 public void load(String url, String size) {57 driver.get(url);58 if (size != null) {59 String[] dimensions = size.split("x");
autoAdjustBrowserWindowSizeToFitViewport
Using AI Code Generation
1import com.galenframework.browser.Browser;2import com.galenframework.browser.BrowserSize;3import com.galenframework.browser.BrowserType;4import com.galenframework.reports.GalenTestInfo;5import com.galenframework.reports.TestReport;6import com.galenframework.reports.model.LayoutReport;7import com.galenframework.specs.page.PageSpec;8import com.galenframework.utils.GalenUtils;9import com.galenframework.validation.ValidationResult;10import org.openqa.selenium.WebDriver;11import org.openqa.selenium.chrome.ChromeDriver;12import org.openqa.selenium.chrome.ChromeOptions;13import org.openqa.selenium.firefox.FirefoxDriver;14import org.openqa.selenium.firefox.FirefoxOptions;15import org.openqa.selenium.remote.DesiredCapabilities;16import org.testng.annotations.AfterTest;17import org.testng.annotations.BeforeTest;18import org.testng.annotations.DataProvider;19import org.testng.annotations.Test;20import java.io.IOException;21import java.util.Arrays;22import java.util.LinkedList;23import java.util.List;24import java.util.Map;25import static com.galenframework.reports.GalenTestInfo.fromString;26import static com.galenframework.validation.ValidationResultListener.validationListener;27import static java.util.Arrays.asList;28import static java.util.Collections.singletonList;29import static java.util.stream.Collectors.toList;30import static org.openqa.selenium.remote.BrowserType.CHROME;31import static org.openqa.selenium.remote.BrowserType.FIREFOX;32public class GalenTest {33 private WebDriver driver;34 private TestReport report;35 private List<Browser> browsers = new LinkedList<>();36 public void setUp() {37 report = new TestReport("Galen Test Report");38 browsers.add(new Browser(CHROME, new BrowserSize(1024, 768)));39 browsers.add(new Browser(FIREFOX, new BrowserSize(1024, 768)));40 }41 public void tearDown() throws IOException {42 report.getReport().save("target/galen-test-report");43 }44 @Test(dataProvider = "browsers")45 public void test(Browser browser) throws IOException {46 GalenTestInfo test = fromString("Galen Test", Arrays.asList("desktop", "mobile"));47 test.getReport().layout(loadLayoutSpec("specs/homepage.spec"), browser.getDriver());48 report.tests().put(test.getName(), test);49 }50 @DataProvider(name = "browsers")51 public Object[][] browsers() {
autoAdjustBrowserWindowSizeToFitViewport
Using AI Code Generation
1package galen.tests;2import com.galenframework.browser.Browser;3import com.galenframework.browser.BrowserFactory;4import com.galenframework.reports.GalenTestInfo;5import com.galenframework.reports.HtmlReportBuilder;6import com.galenframework.reports.model.LayoutReport;7import com.galenframework.specs.page.PageSpec;8import com.galenframework.utils.GalenUtils;9import org.openqa.selenium.Dimension;10import org.openqa.selenium.WebDriver;11import org.openqa.selenium.chrome.ChromeDriver;12import org.openqa.selenium.firefox.FirefoxDriver;13import org.openqa.selenium.remote.DesiredCapabilities;14import java.io.IOException;15import java.util.LinkedList;16import java.util.List;17import static com.galenframework.reports.model.LayoutReport.Status.ERROR;18public class GalenTest {19 public static void main(String[] args) throws IOException {20 WebDriver driver = new FirefoxDriver();21 driver.manage().window().maximize();22 PageSpec pageSpec = new PageSpec("galen.spec");23 LayoutReport layoutReport = GalenUtils.checkLayout(driver, pageSpec, null);24 GalenTestInfo test = GalenTestInfo.fromString("Galen Test");25 test.getReport().layout(layoutReport, "check layout");26 List<GalenTestInfo> tests = new LinkedList<GalenTestInfo>();27 tests.add(test);28 HtmlReportBuilder reportBuilder = new HtmlReportBuilder();29 reportBuilder.build(tests, "target/galen-html-reports");30 driver.quit();31 }32}
autoAdjustBrowserWindowSizeToFitViewport
Using AI Code Generation
1import com.galenframework.utils.GalenUtils2GalenUtils.autoAdjustBrowserWindowSizeToFitViewport(driver, 100, 50)3import com.galenframework.utils.GalenUtils4GalenUtils.autoAdjustBrowserWindowSizeToFitViewport(driver, 100, 50, 10)5import com.galenframework.utils.GalenUtils6GalenUtils.autoAdjustBrowserWindowSizeToFitViewport(driver, 100, 50, 10, 10)7import com.galenframework.utils.GalenUtils8GalenUtils.autoAdjustBrowserWindowSizeToFitViewport(driver, 100, 50, 10, 10, 10)9import com.galenframework.utils.GalenUtils10GalenUtils.autoAdjustBrowserWindowSizeToFitViewport(driver, 100, 50, 10, 10, 10, 10)11import com.galenframework.utils.GalenUtils12GalenUtils.autoAdjustBrowserWindowSizeToFitViewport(driver, 100, 50, 10, 10, 10, 10, 10)13import com.galenframework.utils.GalenUtils14GalenUtils.autoAdjustBrowserWindowSizeToFitViewport(driver, 100, 50, 10, 10, 10, 10, 10, 10)15import com.galenframework.utils.GalenUtils16GalenUtils.autoAdjustBrowserWindowSizeToFitViewport(driver, 100, 50, 10, 10, 10, 10, 10, 10, 10)17import com.galenframework.utils.GalenUtils18GalenUtils.autoAdjustBrowserWindowSizeToFitViewport(driver,
autoAdjustBrowserWindowSizeToFitViewport
Using AI Code Generation
1import com.galenframework.utils.GalenUtils;2public void test() throws IOException, URISyntaxException {3 final String specPath = "specs/adjustingBrowserWindowSizeToFitViewport.gspec";4 WebDriver driver = new ChromeDriver();5 GalenUtils.autoAdjustBrowserWindowSizeToFitViewport(driver, specPath, 100);6 driver.quit();7}8import com.galenframework.utils.GalenUtils;9public void test() throws IOException, URISyntaxException {10 final String specPath = "specs/adjustingBrowserWindowSizeToFitViewport.gspec";11 WebDriver driver = new ChromeDriver();12 GalenUtils.autoAdjustBrowserWindowSizeToFitViewport(driver, specPath, 100, 100);13 driver.quit();14}15import com.galenframework.utils.GalenUtils;16public void test() throws IOException, URISyntaxException {17 final String specPath = "specs/adjustingBrowserWindowSizeToFitViewport.gspec";18 WebDriver driver = new ChromeDriver();
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!