This site is in read only mode. Please continue to browse, but replying, likes, and other actions are disabled for now.
8 / 8
Feb 2019

I have a scenario, where I have to click on either thumb up or thumb up and I have 5 questions like that. And both the id for thumb up and thumb down are different. Could you please let me know how to write a script on how to select a thumb randomly every time for all the question. Kindly share some sample. Thanks

  • created

    Feb '19
  • last reply

    Feb '19
  • 7

    replies

  • 458

    views

  • 3

    users

  • 3

    links

Try to generate a random ID every time you run the script, something like:
String index = “01234”;
randomIndex = RandomStringUtils.random(1, index);

and then use it to tap on it:
driver.findElementsById(“yourID”).get(randomIndex).click();

@Zuzeac: I don’t know if this help or not, because I have 2 different id’s here.
so, I have a thumbs up with a id = “like” and a thumbs down with an id = “unlike”, I have to click on either of them, so i wanted to know how to click on one of them randomly?

@Mohan_Mukherjee No sure if it works, this is what i have in mind right now , just give it a try, so for each question you should load a number between 0 and 1 and based on that you click on like if random number is 0 and Unlike if random number is 1. Continue like this for each question.

String numbers = "01";

	String firstQuestion = RandomStringUtils.random(1, numbers);
	if(firstQuestion.equals(0)) {
		driver.findElementsById("Like").get(0).click();
	}else if(firstQuestion.equals(1)) {
		driver.findElementsById("unLike").get(0).click();
	}
	
	String secondQuestion = RandomStringUtils.random(1, numbers);
	if(secondQuestion.equals(0)) {
		driver.findElementsById("Like").get(1).click();
	}else if(secondQuestion.equals(1)) {
		driver.findElementsById("unLike").get(1).click();
	}
	
	String nQuestion = RandomStringUtils.random(1, numbers);
	if(nQuestion.equals(0)) {
		driver.findElementsById("Like").get(n).click();
	}else if(nQuestion.equals(1)) {
		driver.findElementsById("unLike").get(n).click();
	}

@Zuzeac: It did not work for me. Any other solution you can suggest me? Is it possible to make a 2D object and then try it?