• Slider Banner

    Prepare for

    Technical Interview

    Over .1 Million+ Interview Questions for different profiles

    Read More
  • Slider Banner

    Join the

    Programming World

    Learn about top programming languages, tools and frameworks.

    Read More
  • Slider Banner

    Prepare with

    Certification MCQ

    Practice for certification from 1000+ multiple choice questions.

    Read More

How do perform drag and drop using Selenium WebDriver?

Use Action Class:

Actions act = new Actions(driver);
WebElement source = driver.findElement(By.xpath("")); //source element which you want to drag
WebElement target = driver.findElement(By.xpath("")); //target where you want to drop
act.dragAndDrop(source, target).perform();

How do you launch IE/Firefox/Chrome browser using Selenium?

Check below code:

WebDriver driver;

if(browser.equals("Chrome"))
{	
    System.setProperty("webdriver.chrome.driver", Constants.CHROME_EXE);
    driver = new ChromeDriver();
}
else if(browser.equals("Firefox")) 
{
    System.setProperty("webdriver.gecko.driver", Constants.FIREFOX_EXE);
    driver = new FirefoxDriver();
}
else if(browser.equals("IE"))
{
    System.setProperty("webdriver.ie.driver","Constants.IE_EXE");
    driver = new InternetExplorerDriver();
}

How do you handle alert in Selenium?

To handle alert pop-ups, we need to 1st switch control to alert then click on ok or cancel then move control back to main page.

String mainPage = driver.getWindowHandle();

Alert alt = driver.switchTo().alert(); // to move control to alert popup
alt.accept(); // to click on ok.
alt.dismiss(); // to click on cancel.

//Then move the control back to main web page
driver.switchTo().window(mainPage);

What is Dynamic XPath in Selenium?

Dynamic XPath is also called as custom XPath and it is one way to locate element uniquely.  

Dynamic XPath is used to locate exact attribute or decrease the number of matching nodes/result from a webpage and following XPath expressions can be used for the same:

  • Basic XPath
  • Using ‘OR’ & ‘AND’
  • Using Contains()
  • Using Starts-With
  • Using Text()
  • Using Index
  • Using Chained XPath
  • XPath Axes
  • Following
  • Following Sibling
  • Preceding
  • Preceding Sibling
  • Child
  • Parent
  • Descendants
  • Ancestors

Example

.//*[@class='product']//h4[contains(text(),'Text')]//ancestor::div[@class='table-good']
/li[@class="b"]//select[@name="s"]//option[contains (text(), 'title')]/preceding-sibling::option
//*[text()=’Text']//ancestor::div[@class='table-goods']

Given an array of ints length 3, return the sum of all the elements.

Check below code:

package com.test.arrays;

public class ThreeElementsSum 
{
	public static void main(String args[])
	{
		int sum = ThreeElementsSum.sum3(new int[] {1,2,3});
		System.out.println(sum);
	}
	
	public static int sum3(int[] nums)
	{
		return nums[0] + nums[1] + nums[2];
	}
}

Output

6

Given two strings, a and b, return the result of putting them together in the order abba, e.g. "Hi" and "Bye" returns "HiByeByeHi".

Check below code:

package com.test.strings;

public class abba 
{
	public static void main(String args[])
	{
		System.out.println(abba.makeAbba("Hi","Bye"));
		System.out.println(abba.makeAbba("Programs","Buzz"));
	}

	public static String makeAbba(String a, String b)
	{
		return(a+b+b+a);
	}
}

Output

HiByeByeHi
ProgramsBuzzBuzzPrograms

Modify and return the given map as follows: if the key "a" has a value, set the key "b" to have that value, and set the key "a" to have the value "". Basically "b" is a bully, taking the value and replacing it with the empty string.

Check below code:

package com.test.collections;

import java.util.HashMap;
import java.util.Map;

public class MapBully 
{
	public static void main(String args[])
	{
		MapBully mb = new MapBully();
		HashMap<String, String> hm = new HashMap<String, String>();
		hm.put("a","candy");
		hm.put("b","dirt");
		System.out.println(mb.mapBully(hm));
	}

	public Map<String, String> mapBully(HashMap<String, String> map) 
	{
        if(map.containsKey("a"))
        {
        	map.put("b", map.get("a"));
        	map.put("a", "");
        }
        return map;
    }
}

Output

{a=, b=candy}

Subscribe to