This article will describe the concept of tight and loose coupling in java.
Coupling :- Degree of dependency of Class A on Class B.
How often do changes in one class force changes in another class.
Tight Coupling :- Two classes often change together
If class A knows more than it should about the way in which class B
was implemented, then A and B are tightly coupled.
Loose Coupling :-
Reducing the dependencies of a class that uses the different classes directly.
If the only knowledge that class A has about class B, is
what class B has exposed through its interface, then class
A and class B are said to be loosely coupled.
Example of Tight coupling
class A1
{
A1(int x , int y)
{
System.out.println("sum is " + (x+y));
}
void sam()
{
System.out.println("i am method of class A");
}
}
class B extends A1
{
B(int x, int y) //have to add this constructor other wise while creating the object , compile time error will appear.
{
super(x, y);
}
void sam()
{
System.out.println("i am method of class B");
}
}
public class Coupling
{
public static void main(String args[])
{
B obj = new B(7, 8);
obj.sam();
}
}
Example of loose coupling
interface mycoup
{
void sample();
}
class A1 implements mycoup
{
A1(int x , int y)
{
System.out.println("sum is " + (x+y));
}
@Override
public void sample() {
System.out.println("i am method of class A");
}
}
class B implements mycoup
{
public void sample() {
System.out.println("i am method of class B");
}
}
public class Coupling
{
public static void main(String args[])
{
B obj = new B();
obj.sample();
A1 obj1 = new A1(3, 7);
obj1.sample();
}
}
Output:-
i am method of class B
sum is 10
i am method of class A
Hello Friends,
In this tutorial we will learn how to perform upload operation in selenium.
We can perform the upload operation in selenium by two ways : 1). First by SendKeys() method. 2). Second by using the robot class. First Method :
Precondition : Element should be input and type should be "file", as shown below :
Now we just need to locate the element and just use the method sendKeys() and in sendKeys() method we will pass the path of that file. Syntax :-
driver.findElement(By.xpath("path of element")).sendKeys("path on file");
public class Uploadfile
{
WebDriver driver;
@BeforeTest
public void g() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\cchauhan\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
/*FirefoxOptions foptions = new FirefoxOptions();
foptions.setCapability("marionette", false);
driver = new FirefoxDriver(foptions);*/
driver.manage().window().maximize();
driver.navigate().to("http://www.way2testing.com/p/this-webpage-is-designed-for-selenium.html");
Thread.sleep(3000);
}
@Test
public void h()
{
//Remember element should be input and type should be "file"
driver.findElement(By.xpath(".//*[@id='post-body-2064404811754288590']/form[1]/input[1]")).sendKeys("C:\\Users\\cchauhan\\Desktop\\wallpaper.png");
}
}
Second Method : By using Robot Class:- What is Robot class ?
"Basically robot class will simulate the Keyboard and mouse actions." Steps :- 1). First Copy the file path. 2).Paste the file path in popup window 3).Press Enter button.
by robot class we can perform CTRL + C operation by below methods as :-
StringSelection ss = new StringSelection("C:\\Users\\cchauhan\\Desktop\\wallpaper.png");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
by robot class we can perform CTRL + V operation as below :-
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
by robot class we can press enter key as below :-
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Here is sample code :-
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Uploadfile
{
WebDriver driver;
@BeforeTest
public void g() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\cchauhan\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
/*FirefoxOptions foptions = new FirefoxOptions();
foptions.setCapability("marionette", false);
driver = new FirefoxDriver(foptions);*/
driver.manage().window().maximize();
driver.navigate().to("http://www.way2testing.com/p/this-webpage-is-designed-for-selenium.html");
Thread.sleep(3000);
}
@Test
public void h() throws AWTException
{
driver.findElement(By.xpath(".//*[@id='post-body-2064404811754288590']/form[1]/input[1]")).click();
Robot robot = new Robot();
robot.setAutoDelay(2000);
StringSelection ss = new StringSelection("C:\\Users\\cchauhan\\Desktop\\wallpaper.png");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
robot.setAutoDelay(2000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
}