I want to launch the selenium code on my mac.
With IntelliJ, I can launch each tests through the interface (thanks to the green button on the left).
I need now to launch selenium through cli, how to I do ?
My folder structure : /project/src/test/java/project_test/Tests.java
(I'm trying this right now : https://docs.seleniumhq.org/selenium-ide/docs/en/introduction/command-line-runner/, but as I have my driver inside my function I don't know if it will work, I'll let you know)
One example of my test :
public void TestA() throws InterruptedException, MessagingException {
String nameError = "TestA";
System.setProperty("webdriver.chrome.driver", locateDriver);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.navigate().to(url + "route");
Thread.sleep(3000);
driver.findElement(By.cssSelector(".primary > a[href=\""+url+"connexion/connexion/index/\"]")).click();
driver.findElement(By.id("login-client")).sendKeys("40003099");
driver.findElement(By.id("login-cp")).sendKeys("281101");
driver.findElement(By.xpath("//button[contains(text(),'Connexion')]")).click();
Thread.sleep(5000);
WebElement strvalue = driver.findElement(By.xpath("//*[#id=\"maincontent\"]/div[1]/div[2]/div/div/div"));
Thread.sleep(3000);
String expected = "some text";
String actual = strvalue.getText();
System.out.println(actual);
if(expected.equals(actual)){
System.out.println("Ok");
}
else {
System.out.println("Not ok");
sendMail(nameError);
}
Thread.sleep(5000);
driver.close();
}
Are you trying to run java file using external jar ? if yes, maybe this can help.
First, go to your directory :
cd project
MacBook-Pro: project$
Second, run this command :
java -cp .:yourFullPathExternalJar\*: src.test.java.project_test.Tests
Hope this helps
Related
I am trying to handle multiple browser windows using selenium webdriver, but getting java.util.NoSuchElementException. A week ago this code was working fine but now getting issue in the code while trying to switch to third window.
The issue I am getting on line String Third_window = iterate.next();
Note: This code was working fine properly a week ago.
public class Firefox {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver",
"C:\\Users\\singhais\\Documents\\Selenium Prerequisites\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("https://www.hdfcbank.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// First window
System.out.println("GettingID for first Window");
Set<String> win = driver.getWindowHandles();
Iterator<String> iterate = win.iterator();
String first_window = iterate.next();
System.out.println(first_window);
driver.findElement(By.xpath("//*[#id='element2']/div[1]/div[3]/div[2]/a/img")).click();
// second window
System.out.println("GettingID for second Window");
win = driver.getWindowHandles();
iterate = win.iterator();
first_window = iterate.next();
System.out.println(first_window);
System.out.println(driver.getTitle());
String second_window = iterate.next();
System.out.println(second_window);
driver.switchTo().window(second_window);
Thread.sleep(10000);
System.out.println(driver.getTitle());
driver.findElement(By.xpath("//*[#id='wrapp']/div[2]/div[3]/div[1]/div/div[2]/ul/li[1]/a")).click();
// ThirWindow
System.out.println("GettingID for third Window");
win = driver.getWindowHandles();
iterate = win.iterator();
first_window = iterate.next();
System.out.println(first_window);
second_window = iterate.next();
System.out.println(second_window);
String Third_window = iterate.next();
System.out.println(Third_window);
driver.switchTo().window(Third_window);
Thread.sleep(10000);
System.out.println(driver.getTitle());
Thread.sleep(3000);
driver.close();
driver.switchTo().window(second_window);
Thread.sleep(3000);
driver.close();
driver.switchTo().window(first_window);
driver.findElement(By.xpath("html/body/div[1]/div[1]/div[1]/div/div[2]/ul/li[2]/a")).click();
Thread.sleep(6000);
driver.close();
}
}
This code is not duplicate of the problem mentioned in given link below. In my issue I am getting NoSuchElementException while trying to navigate to last window.
Using Thread.sleep(3000); Your program works fine for me.I just wait 3s for getting next windows
System.out.println("GettingID for third Window");
Thread.sleep(3000);
win = driver.getWindowHandles();
iterate = win.iterator();
Please check and let me know
I am trying to write a Selenium test against Amazon site. I want to get "Sign in" element so that I can click on it.
url: www.amazon.es
Here is my Selenium Code:
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.es");
try
{
driver.findElement(By.id("nav-link-accountList")).click();
}
catch (Exception e)
{
System.out.println("Not Found");
}
Sometimes the code works correctly but sometimes it does not find the ID "nav-link-yourAccount". What is the problem? and how can I solve it?
Provide few seconds of wait, before click to this webelement so your driver may able to find the webelement.
For wait i am using Explicit wait method.
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("nav-link-accountList"))));
driver.findElement(By.id("nav-link-accountList")).click();
The element which you are trying to click with id as nav-link-yourAccount is not clickable. To proceed further you need to click either the link with text Hola. Identifícate or the link with text Mi cuenta using one of the following xpaths:
//a[#id='nav-link-yourAccount']/span[text()='Hola. Identifícate']
or
//a[#id="nav-link-yourAccount"]/span[contains(text(),'Mi cuenta')]
Instead using implicit wait try using explicit wait for the login element.
I've tried with explicit wait over 50 click and it did works.
Here is code you can use.
public class dump {
public static void main(String a[]){
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 15);
for(int i=0; i<=50; i++){
driver.get("https://www.amazon.es");
try{
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='nav-link-accountList']")));
driver.findElement(By.xpath("//*[#id='nav-link-accountList']")).click();
System.out.println("clicked\t"+i);
}catch (Exception e){
e.printStackTrace();
System.out.println("Not Found");
}
}
}
}
Here is the proof of run:
All the best!!
Apply wait until element is appeared, so that it avoids NoSuchElementException and code is working without any error.
Below code is working fine:
driver.get("https://www.amazon.es");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement accontButton=driver.findElement(By.id("nav-link-accountList"));
WebDriverWait waitforelement=new WebDriverWait(driver,20);
waitforelement.until(ExpectedConditions.elementToBeClickable(accontButton));
try{
accontButton.click();
}
catch (Exception e){
System.out.println("Not Found");
}
Have you tried to find elements by xpath?
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.es");
try
{
driver.findElement(By.xpath("//*[#id='nav-link-accountList']")).click();
}catch (Exception e)
{
System.out.println("Not Found");
}
I have a URL with a video embedded to it just like youtube URLs and i want to validate if the video loads and streams
The difficulty i am having is that i don't have the tagname , id or anything of the video element , so how can i check in such a case
Code using selenium:
public class URLCheck
{
public static void main(String args[]){
File file = new File("C:\\Users\\MB0000038\\Desktop\\chromedriver\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
WebDriver driver=new ChromeDriver();
driver.get("https://www.youtube.com/watch?v=FtsrtcagbOQ");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// JavascriptExecutor js = (JavascriptExecutor) driver;
// WebElement video = driver.findElement(By.tagName("video"));
driver.quit();
}
}
This code works well but only open a chrome tab displaying the video
Thanks in advance
I have checked your video, there are two tags which you can play around to verify your video.
driver.get("https://www.youtube.com/watch?v=59rEMnKWoS4");
driver.manage().window().maximize();
WebElement pauseAndPlay = driver.findElement(By.xpath("//button[#class='ytp-play-button ytp-button']"));
//Get the attribute aria-lable of pauseAndPlay Element which would tells you current state of video(pause/play)
String videoState=pauseAndPlay.getAttribute("aria-label");
if(videoState.equalsIgnoreCase("Pause")){
System.out.println("Video is currently in play state");
Thread.sleep(3000);
//pausing my video and checking the current play time
pauseAndPlay.click();
Thread.sleep(2000);
WebElement currentTimeElement=driver.findElement(By.xpath("//span[#class='ytp-time-current']"));
String currentTime=currentTimeElement.getText();
if(currentTime!="0:00"){
System.out.println("my video is getting progressed and currently at:"+currentTime);
}else{
System.out.println("my video is not getting played");
}
}else if(videoState.equalsIgnoreCase("Play")){
System.out.println("Video is currently paused");
}
WebElement element=driver.findElement(By.name("file"));
element.click();
element.sendKeys("C:\Users\Minesh\Desktop\arch_logo.png);
The Above test case is just to select the file.
we have another button to upload the file.
If we run the program,only the window is getting pop up.
File is not getting selected.
And the input type is of button.
Please guide for the query
You need to escape each backslash so it can be considered as a file path:
driver.findElement(By.name("file"))
.sendKeys("C:\\Users\\Minesh\\Desktop\\arch_logo.png);
#user6203568 - You can update your code as below:
It should work as it is working for me. And give your test method priority as per your requirement. Just for Example I gave priority here as #Test(priority = 1). I hope it should work for you.
#Test(priority = 1)
public void CERTIFICATIONSSCREENUploadCertficationFilesValidation()
throws InterruptedException, AWTException {
//Click on File Upload Button
driver.findElement(By.xpath("//*[#id='certificationFile']")).click();
Thread.sleep(1000);
// Set the file name in the clipboard. Also following line of code will search file in your computer so make sure you provide correct file path.
StringSelection s = new StringSelection("C:\\Doc\\CertificationFile.xls");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(s, null);
Thread.sleep(1000);
Robot robot1 = new Robot();
robot1.keyPress(KeyEvent.VK_ENTER);
robot1.keyRelease(KeyEvent.VK_ENTER);
robot1.keyPress(KeyEvent.VK_CONTROL);
robot1.keyPress(KeyEvent.VK_V);
robot1.keyRelease(KeyEvent.VK_V);
robot1.keyRelease(KeyEvent.VK_CONTROL);
robot1.keyPress(KeyEvent.VK_ENTER);
robot1.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(1000);
}
I have a test ready to be executed but it takes a long time to finish. In this test I'm feeding in csv data, so basically the whole test will run 56 times. I was wondering if there's anyway I could use multiple browser instance and divide the workload to four instance. It will save me some time. I tried to use TestNG's ThreadPoolSize but it's not doing what I want it to. It's using the same data for four instances of firefox. I want each browser to have it's own unique data. Please check my code and let me know what I'm missing. I really appriciate every one's help.
public class StudentPageTest {
WebDriver driver;
DesiredCapabilities capability;
WebElement element;
WebDriverWait wait;
private String baseURL;
#BeforeTest
public void setUp() throws MalformedURLException{
//capability = DesiredCapabilities.firefox();
//driver = new FirefoxDriver();
//wait = new WebDriverWait(driver, 120);
//driver.manage().deleteAllCookies();
baseURL = "http://somewebsite.com";
}
#SuppressWarnings("resource")
#Test(threadPoolSize = 4)
public void StudentPortalTest() throws InterruptedException, IOException{
driver = new FirefoxDriver();
wait = new WebDriverWait(driver, 120);
driver.manage().deleteAllCookies();
String studentId = "studentID.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
br = new BufferedReader(new FileReader(studentId));
while ((line = br.readLine()) != null) {
String[] student_id = line.split(cvsSplitBy);
//Logging in Student Portal---------------------------------------------------------------------------------------------------------------|
for (int i = 0; i < student_id.length; i++) {
driver.get(baseURL+student_id[i]);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.cssSelector(".logo>img")).isDisplayed();
driver.findElement(By.cssSelector("#UserName")).sendKeys("SecretUserName");
driver.findElement(By.cssSelector("#Password")).sendKeys("EvenMoreSecretPassword");
driver.findElement(By.cssSelector(".submitBtn")).click();
driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
Thread.sleep(4000);
...............and the test goes on below...................
}
#AfterTest
public void tearDown(){
driver.quit();
}
}
You can let the Selenium grid do the task of distribution. Use TestNG to run your cases in parallel using dataprovider. Use a dataprovider to read your csv and pass one data to one #Test.
Set dataprovider thread count in your suite xml and make sure you set #DataProvider(parallel=true)
Going to your code, replace your call of instantiating a firefoxdriver to remotewebdriver to use the grid. Make sure your driver is not at class level but is a threadlocal : threadlocal<driver>.
I was trying many different ways to achieve the same thing you are trying to do. After a lot of trial and error I finally came across a solution that worked for me.
You need Selenium Grid You can then set up a few nodes to run your different browsers from
You need TestNG like you have been using but instead of trying to use threads, you will use an .xml file to pass in parameters to your test which will run the one test over and over in parallel.
If you read this document carefully and follow the steps, you should be able to achieve the same result
#Test(threadPoolSize = 4, invocationCount = 4)