Published March 29, 2021 by with 0 comment

Different Approaches For Maximizing Browser Window In Selenium Webdriver

Hello learners,  

Maximizing the browser window manually is a very simple task. But when we do it through Automation then there are number of ways we can do this. Generally, when the questions like what are the different ways we can maximize a window in Selenium? or What are different approaches for maximizing window in Selenium? asked during interviews, we ended up giving answers like by using maximize() method of the driver. But apart from that below are the different approaches that we can use for maximizing browser window in Selenium WebDriver.


Approch 1. Using maximize() Method:

We have different methods under driver.manage().window(). 

So, here we will use maximize() method to maximize browser window. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public class UsingMaximizeFunction {

	public static void main(String[] args) {

		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();

		// Maximize current window
		driver.manage().window().maximize();

		// Navigate to a website
		driver.get("https://www.myautomationlab.com/");

		// Close the browser
		driver.quit();

	}

}

Approch 2. Using fullscreen() Method:

As mentioned in Approch 1, we have different methods under driver.manage().window(). 

So, here we will use fullscreen() method to maximize browser window. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class UsingFullScreenFunction {

	public static void main(String[] args) throws InterruptedException {

		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();

		// Navigate to a website
		driver.get("https://www.myautomationlab.com/");

		// Maximize current window using "fullscreen" function
		driver.manage().window().fullscreen();

		Thread.sleep(3000);

		// Close the browser
		driver.quit();
	}

}

Approch 3. Using  setSize() Method:

Dimension class is used to set height and width of the object. 

So, here we can set the height and width of the browser as per our requirement. 

To do browser window maximization one should know the height and width of the maximize window. This will differ based on the screen resolution of your device. 

To use dimension class first create the object of Dimension class. 

Then pass that dimension object to setSize() method as driver.manage().window().setSize(dimension object) method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class UsingWindowSize {

	public static void main(String[] args) {

		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();

		// Maximize current window by setting windows size
		Dimension dim = new Dimension(1280, 860);
		driver.manage().window().setSize(dim);

		// Navigate to a website
		driver.get("https://www.myautomationlab.com/");

		// Close the browser
		driver.quit();

	}

}

Approch 4. Using ChromeOptions window-size:

ChromeOptions class is used for customizing the ChromeDriver session. 

So, when we launch any browser using Selenium then it opens up with given configurations. 

First, create the ChromeOptions class object. 

Then add arguments to that object as chromeOptions.addArguments("window-size=1280,860"). Here 1280 is the width and 860 is the height for maximizing a window. You can pass width and height as per your requirement there. 

Then while doing Webdriver initialization we need to pass the instance of ChromeOptions class. 

After that, it will launch the browser with maximize browser window. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class UsingChromeOptionsWindowSize {

	public static void main(String[] args) {

		// Maximize current window using ChromeOptions "window-size"
		ChromeOptions chromeOptions = new ChromeOptions();
		chromeOptions.addArguments("window-size=1280,860");

		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver(chromeOptions);

		// Navigate to a website
		driver.get("https://www.myautomationlab.com/");

		// Close the browser
		driver.quit();

	}

}

Approch 5. Using ChromeOptions --start-fullscreen:

We can use --start-fullscreen method of ChromeOptions. 

First, create the ChromeOptions class object. 

Then add arguments to that object as chromeOptions.addArguments("--start-fullscreen"). Here "--start-fullscreen" is a keyword that you have to use as it is.

Then while doing Webdriver initialization we need to pass the instance of ChromeOptions class. 

After that, it will launch the browser with maximized browser window. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class UsingChromeOptionsStartFullscreen {

	public static void main(String[] args) {

		// Maximize current window using ChromeOptions "--start-fullscreen"
		ChromeOptions chromeOptions = new ChromeOptions();
		chromeOptions.addArguments("--start-fullscreen");

		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver(chromeOptions);

		// Navigate to a website
		driver.get("https://www.myautomationlab.com/");

		// Close the browser
		driver.quit();

	}

}

Approch 6. Using ChromeOptions --start-maximized/--kiosk:

We can use --start-maximized/--kiosk method of ChromeOptions. 

First, create the ChromeOptions class object. 

Then based upon our operating system we need to add arguments to that object.

If the operating system is windows then add arguments to ChromeOption object as chromeOptions.addArguments("--start-maximized"). Here "--start-maximized" is a keyword that you have to use as it is.

If the operating system is MAC/Linux then add arguments to ChromeOption object as chromeOptions.addArguments("--kiosk"). Here "--kiosk" is a keyword that you have to use as it is.

Then while doing Webdriver initialization we need to pass the instance of ChromeOptions class. 

After that, it will launch the browser with maximized browser window.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class UsingChromeOptionsStartMaximized {

	public static void main(String[] args) {

		ChromeOptions chromeOptions = new ChromeOptions();

		if (System.getProperty("os.name").startsWith("Windows")) {

			// To maximize window in Windows system use "--start-maximized"
			chromeOptions.addArguments("--start-maximized");
		} else {

			// To maximize window in Mac & linux system use "--kiosk"
			chromeOptions.addArguments("--kiosk");
		}

		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver(chromeOptions);

		// Navigate to a website
		driver.get("https://www.myautomationlab.com/");

		// Close the browser
		driver.quit();

	}

}

The Source Code for above topic is available on GitHub Repository, You can get it from this URL:- 

Read More
Published March 27, 2021 by with 0 comment

Some Cool Facts About Main Method in Java

Hello learners,  

In this post, we'll learn about some basic concepts related to Main method in Java. But before that lets understand what is main method, its importance, how it interacts with JVM etc. 

1. What is main method in Java?

Main method is the entry point for every Java program. When you execute certain class that time Java compiler first search for main method for that class, after finding the main method it starts it's execution. Below is the figure for illustrating meaning of every word in declaration of  main method.

                

                 

In the above figure public is  a Access Modifier that means the main method is called by JVM from any class, package, subclass etc. static is a Keyword that means we don't need to create objects of main method it can be directly called with the help of class. Void is a return type that means main method does not return any value. main is a method name. And lastly main method can accept one argument of type String Array.

2. What happens to Java program if main method is not present.

It will give byte code verification error as "Main method not found in class".  Now, this will be a error and not exception since the program has not started its execution yet. Prior to JDK7 main method declaration was not mandatory but after JDK7 it is mandatory to define method for execution of program.

3. Can we overload main method in Java?

Yes, we can surely do this. We can declare multiple methods with the name as main but the parameters will be different. Let's say we have declare four different main method with different parameters as String array, Single String, Single Integer and with two Integer. And calling the other mains from first main method. Now, since the data type of parameters are different in every case so the main method execution is happening properly.

Code: 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package javaProgramsforInterview;

public class JavaPrograms {
	public static void main(String[] args) {

		System.out.println("Main Method-1");
		main("my automation lab");
		main(50);
		main(20, 40);
	}

	public static void main(String args) {

		System.out.println("Main Method-2");
	}

	public static void main(int a) {

		System.out.println("Main Method-3");
	}

	public static void main(int a, int b) {

		System.out.println("Main Method-4");
	}
}

Output:

1
2
3
4
Main Method-1
Main Method-2
Main Method-3
Main Method-4

4. Can we call main method from another class?

Yes, we can surely do this. Since main method is static in nature and static methods we can call by using ClassName.method() from another classes. 

Code: 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package javaProgramsforInterview;

public class ClassA {

	public static void main(String[] args) {

		System.out.println("This is Java Class A");
		ClassB.main(args);
	}

}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package javaProgramsforInterview;

public class ClassB {

	public static void main(String[] args) {

		System.out.println("This is Java Class B");

	}

}

Output:

1
2
This is Java Class A
This is Java Class B

5. What happens if we call main methods of two classes within each other

When we call main methods of two classes within each other then the output will go in infinite loop. In the above example let's say we are calling main method of ClassB in ClassA and main method of ClassA in ClassB then when we execute class A first it will execute all the methods for A and then the control will go to ClassB it will execute all methods of ClassB but since in ClassB we have called main method of ClassA then the control will again go to ClassA and it will continue till infinite loop.

6. Can we Override the main method?
No, we can't do this. Since main method is static in nature we can not override it. In method overriding we change the implementation part of methods having same name. Now, for changing the implementation part we need to create objects within that method but for static methods we don't need to create its object it gets directly called by it's class name. Suppose, if we have multiple main methods with method name as "main" and same parameters then it will be very confusing that which main method to be executed. Therefore, it is not possible to override a main() method.

Read More
Published March 23, 2021 by with 0 comment

Removing duplicate characters from given string without using Java Built in functions.

Approch 1

First will convert String to Characters by using toCharArray function. After that will store that CharArray to Set it will automatically remove all duplicates. And then will use StringBuilder function to reconstruct that String.


Code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package javaProgramsforInterview;
 
import java.util.LinkedHashSet;
import java.util.Set;
 
public class RemovingDuplicates1 {
 
	public static void main(String[] args) {
		String string = "samplestringfortest";
 
		char[] chars = string.toCharArray();
		Set<Character> charSet = new LinkedHashSet<Character>();
		for (char c : chars) {
			charSet.add(c);
		}
 
		StringBuilder sb = new StringBuilder();
		for (Character character : charSet) {
			sb.append(character);
		}
		System.out.println(sb.toString());
	}
}
Output:

sampletringfo


Approch 2

First will convert String to Characters by using toCharArray function. After that we will compare n’th letter from zero to (n-1) index to all the letters of string. Then we will use a boolean flag to check the duplicate letter. If a duplicate letter is found then will turn off the flag and will not append the letter to the String. If there is no duplicate letter then we will keep the flag value as true and will append the letter to string. For string reconstruction we are using StringBuilder function.


Code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package javaProgramsforInterview;
 
public class RemovingDuplicates2 {. 
 
	public static void main(String[] args) {
		String string = "samplestringfortest";
		char[] chars = string.toCharArray();
		StringBuilder sb1 = new StringBuilder();
 
		for (int i = 0; i < chars.length; i++) {
			boolean flag = true;
			for (int j = 0; j < i; j++) {
				if (chars[j] == chars[i]) {
					flag = false;
					break;
				}
			}
			if (flag) {
				sb1.append(chars[i]);
			}
		}
		System.out.println(sb1.toString());
	}
}
Output:

sampletringfo

Read More

Popular Posts