Can we use the swipe code on differnt devises

Hi All,

I want to know how to swipe vertical and horizotal.

  1. if i use swipe function on one of the device say(One plus one) will the same code works on the Samsung and other devices?

  2. Can the same swipe function works for emulators as well

if yes. could you please tell me how to use the vertical and horizontal swipe works and the code

Thanks,
Shantha

Yes, same swipe should work. The below code will work irrespective of device screen size because the values are calculated at run time -

/**
	 * Horizontal swipe Pass : Right2Left to swipe from right to left Pass :
	 * Left2Right to swipe from left to right
	 */
	public void swipingHorizontal(String direction) throws InterruptedException {
		// Get the size of screen.
		size = driver.manage().window().getSize();
		log.info(size);

		int startx = (int) (size.width * 0.70);
		int endx = (int) (size.width * 0.30);
		int starty = size.height / 2;
		// Swipe from Right to Left.
		if (direction.contentEquals("Right2Left")) {
			driver.swipe(startx, starty, endx, starty, 3000);
		} else if (direction.contentEquals("Left2Right")) {
			driver.swipe(endx, starty, startx, starty, 3000);
		}
		waitFor(2000);
	}

	/**
	 * Vertical swipe Pass : Bottom2Top to swipe upwards Pass : Top2Bottom to
	 * swipe downwards
	 */
	public void swipingVertical(String direction) throws InterruptedException {
		// Get the size of screen.
		size = driver.manage().window().getSize();

		int starty = (int) (size.height * 0.20);
		int endy = (int) (size.height * 0.80);
		// int bottom = (int) (size.height);
		int startx = size.width / 2;
		// Swipe from Bottom to Top.
		if (direction.contentEquals("Top2Bottom")) {
			driver.swipe(startx, starty, startx, endy, 3000);
		} else if (direction.contentEquals("Bottom2Top")) {
			driver.swipe(startx, endy, startx, starty, 3000);
		}
		waitFor(2000);
	}

I tired this code, for the vertical scroll , it didn’t work. could you help me on the same.

java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)

@slakshmiics : Apologies. I have waitFor() function in my class. Could you please replace waitFor(2000) with below code -

Thread.sleep(2000);

I Dimension size = driver.manage().window().getSize();

	int starty = (int) (size.height * 0.20);
	int endy = (int) (size.height * 0.80);
	// int bottom = (int) (size.height);
	int startx = size.width / 2;
	// Swipe from Bottom to Top.
	if (direction.contentEquals("topToBottom")) {
		driver.swipe(startx, starty, startx, endy, 3000);
	} else if (direction.contentEquals("bottomToTop")) {
		driver.swipe(startx, endy, startx, starty, 3000);
	}
	Thread.sleep(2000);
}

}

this didnot work

@slakshmiics : The above code is the implementation to swipe horizontal or vertial. but to use it during your test you have to define the string parameter. For example. to swipe in vertical direction from top to bottom, you have to call the above function like this -

swipingVertical(“Top2Bottom”);

You call will depends on where you have defined above 2 functions. (swipingVertical and swipingHorizontal). I have defined them in the abstract class and I extend Abstract class in the test class.

Ipublic void ClickOnAssignmentButton() throws InterruptedException{

	while(!AssignIcon.get(0).isDisplayed()){
		
		AssignIcon.get(0).click();
		break;
	}

CommonUtilsObj=new CommonUtils(driver);
CommonUtilsObj.swipingVertical("topToBottom");

}

this is how I am using

Where did you define swipingVertical() method? Is it in CommonUtilsObj class?

yes you are right,this is the class CommonUtils. and calling it from different class

Can you post code for both classes and error logs.

1 Like

Thanks Suman, the scrolling worked

1 Like