Continue test on step failure - java

I have a function -
private boolean selectFromDropDown(String locator, String value) {
try {
new Select(driver.findElement(By.xpath(locator))).selectByVisibleText(value);
return true;
}
catch (Error e) {
verificationErrors.append(e.toString());
System.out.println("Could not find element");
return false;
}
}
I want the function to return true when the action is possible or else return some message and continue to the next step. Now I get an error -
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Cannot locate element with text: Indi
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.24.1', revision: '17205', time: '2012-06-19 16:53:24'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_07'
Driver info: driver.version: unknown
at org.openqa.selenium.support.ui.Select.selectByVisibleText(Select.java:147)
at com.adobe.auto.testcases.utils.SeleniumTest.selectFromDropDown(SeleniumTest.java:89)
at com.adobe.auto.testcases.utils.SeleniumTest.RunSeleniumTest(SeleniumTest.java:66)
at com.adobe.auto.testcases.utils.Excel_Reader.runTest(Excel_Reader.java:653)
at com.adobe.auto.testcases.utils.DriverFinal.main(DriverFinal.java:25)
and the executions stops there.
What do I need to do to make this working as I want.

Try catching rather Exceptions instead of Errors and it should work just fine.
Errors are derived from java.lang.Error, and Exceptions are derived from java.lang.Exception. According to the API
An Error "indicates serious problems that a reasonable application should not try to catch."
An Exception "indicates conditions that a reasonable application might want to catch."

Related

Selenium/Java - Cannot locate option with index

I am practicing selenium with practiceautomation.com site. I have problem with registration - my selector cannot locate option with index.
code:
Select yearSelector = new Select(driver.findElement(By.id("years")));
yearSelector.selectByIndex(2000);
And I got something like:
org.openqa.selenium.NoSuchElementException: Cannot locate option with index: 2000
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'DESKTOP-NN5LV43', ip: '192.168.0.2', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '10.0.1'
Driver info: driver.version: unknown
It is strange, because I use also
Select dateSelector = new Select(driver.findElement(By.id("days")));
dateSelector.selectByIndex(15);
And everything works fine and the day on the list is normally selected
Picture:
Year
As you can see, years are visible.
You try to select by index 2000 and errors says no option with index 2000. Maybe you want to select by value?
Select yearSelector = new Select(driver.findElement(By.id("years")));
yearSelector.selectByValue("2000");
public void selectByIndex(int index)
The above method select the option at the given index. This is done by examining the "index" attribute of an element, and not merely by counting.If no matching option elements are found then NoSuchElementException is thrown
Check for index attribute in html with value as 2000 it should not be there so try with
selector.selectByIndex(1); // see first visible year is selected or not

Scroll and click - Not working for both Android and iOS on Appium tool with selenium java

I am trying to scroll and its not working for both Android and iOS, Can you please help me on this.
Look forward to hear back from you.
Did try with ScrollTO and ScrollToExact as both of them are deprecated now, so did try with this:
String str = "CADILLAC";
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+str+"\").instance(0))").click();
Still does not work.
Did go through many blogs , videos , course content and material no one has explained or there a specific solution on how to handle this
Did try with below,still gets the error
org.openqa.selenium.WebDriverException:
driver.findElementByAndroidUIAutomator("UiSelector().resourceId(\"current_value_tv\").text(\"All Makes\")").click();
driver.findElementByAndroidUIAutomator("UiSelector().className(\"android.widget.TextView\").text(\"AUSTIN HEALEY\")").click();
org.openqa.selenium.WebDriverException: An unknown server-side error
occurred while processing the command. (WARNING: The server did not
provide any stacktrace information)
Command duration or timeout: 45 milliseconds
Build info: version: '2.45.0', revision: '32a636c', time: '2015-03-05 22:01:35'
System info: host: 'NCA1026471', ip: '192.168.56.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version:
'1.8.0_91'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{app=C:\Appium\workspace\Lokesh\app\app-release.apk,
appPackage=au.com.carsguide, rotatable=true,
networkConnectionEnabled=true, noReset=true, warnings={},
handlesAlerts=true, deviceName=Carsguide Product, version=0.17.0,
platform=ANDROID, appActivity=au.com.carsguide.activity.HomeActivity,
desired= {app=C:\Appium\workspace\Lokesh\app\app-release.apk,
appPackage=au.com.carsguide,
appActivity=au.com.carsguide.activity.HomeActivity, noReset=true,
platformVersion=5.0, browserName=, platformName=Android,
deviceName=Carsguide Product, device=Android}, acceptSslCerts=true,
platformVersion=21, automationName=selendroid, browserName=selendroid,
takesScreenshot=true, javascriptEnabled=true, platformName=android,
device=Android}]
Session ID: e90cac4d-38aa-99fd-2dd2-70cc09a0e717
*** Element info: {Using=-android uiautomator, value=UiSelector().resourceId("current_value_tv").text("All Makes")}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method)
you can try this below code, i was trying this code on settings page..
AppiumDriver driver = new AndroidDriver(new URL(""), cap1);
driver.scrollTo("About phone");
Pass the String which is present in the bottom of your app page.
driver.scrollTo("Enter your value");
Use appropriate wait Statements.
use driver.swipe instead of driver.scroll
http://automationbyharsh.blogspot.in/
visit this blog to get full documentation of swipe method for Android and IOS.
scrollTO method is not consistent in appium for android apps. so try the following.
go till model listing page
try the below code snippet , This was for flipkart app
it may help you with your app
driver.findElementById("com.flipkart.android:id/search_autoCompleteTextView").sendKeys("Reebok Shoes" +"\n");
Thread.sleep(8000);
Dimension size = driver.manage().window().getSize();
System.out.println(size);
int Starty=(int)(size.height*0.90);
int Endy=(int)(size.height*0.10);
int Startx=(int)(size.width*0.50);
WebElement ele_item = driver.findElementByClassName("android.widget.TextView");
Thread.sleep(3000);
do
{
List<WebElement> ele_item2 = driver.findElementsByXPath("//*[#index='2'][#text='Reebok SPEED XT Running Shoes'][#class='android.widget.TextView']");
int size2 = ele_item2.size();
System.out.println(size2);
if(size2>0)
{
driver.findElementByXPath("//*[#index='2'][#text='Reebok SPEED XT Running Shoes'][#class='android.widget.TextView']").click();
break;
}
driver.swipe(Startx, Starty, Startx, Endy, 1000);
Thread.sleep(2000);
} while(ele_item.isDisplayed()==true);

org.openqa.selenium.NoSuchElementException - Selenium Test

In Java, I have written a Selenium test for testing Yahoo Movies. I am testing This Movie from yahoo website. But the code that I have written (given below) is throws an Exception (given below). I am new to Selenium, so please solve the problem.
Code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Tests {
WebDriver driver;
Wait<WebDriver> wait;
boolean result;
Tests() {
driver = new FirefoxDriver();
wait = new WebDriverWait(driver, 30);
driver.get("http://www.yahoo.com/");
}
public static void main(String arg[]) {
boolean result = new Tests().movies();
System.out.println(result?"PASSED":"FAILED");
}
public boolean movies() {
try {
System.out.print("Testing Movies... ");
driver.findElement(By.linkText("Movies")).click();
driver.findElement(By.linkText("Finding Dory")).click();
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver webDriver) {
return webDriver.findElement(By.className("yvp-flash-video")) != null;
}
});
return driver.findElement(By.id("Main")).getText().contains("'Finding Dory' Trailer");
}
catch(Exception exp) {
exp.printStackTrace();
return false;
}
}
}
Exception:
Testing Movies... org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Finding Dory"}
Command duration or timeout: 5.08 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'Jahanzeb', ip: '10.99.14.207', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.7.0_79'
*** Element info: {Using=link text, value=Finding Dory}
Session ID: 5f14f1fa-85e4-471e-982f-27317dd766b7
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=46.0.1}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByLinkText(RemoteWebDriver.java:428)
at org.openqa.selenium.By$ByLinkText.findElement(By.java:246)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
at Tests.movies(Tests.java:189)
at Main.main(Main.java:14)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Finding Dory"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'Jahanzeb', ip: '10.99.14.207', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.7.0_79'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/fxdriver#googlecode.com/components/driver-component.js:10770)
at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/fxdriver#googlecode.com/components/driver-component.js:10779)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12661)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12666)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12608)
FAILED
I am assumming that you are trying to click a "link" named "Finding Dory" on this page:
But this is not a link - try to click it manually, it simply is not possible to click on it.
If you click on it, nothing happens, it's just not clickeable - this is nothing but a simple text "Finding Dory".
What you can do is to click a link named "Play trailer" that is located below the text "Finding Dory". The problem is that there are many links with the same name "Play trailer" on this page, and WebDriver doesn't know which link should be clicked, and returns an error if you try a simple method findElement( By.linkText('Play trailer')).click();
You need to tell WebDriver that it should click on the link that is located under the text "Finding Dory". To do it you need more advanced locating strategies than a simle method By.linkText. The two the most popular are locating using xpath or css expressions.
First you need to examine a structure of this page. Open this page in the browser, and press F12 key - this opens Developer Tools window (how to use this tool is beyond this answer). You will see something like this:
That is:
DIV tag that contains a text "Finding Dory"
followed by A tag that contains a text "Play trailer"
You can build an xpath expression for the above, that finds a link "Play trailer" located under a DIV tag that contains "Finding Dory" in this way :
//div[contains(.,'Finding Dory')]/following-sibling::a[contains(.,'Play trailer')]
and then replace this line of your code:
driver.findElement(By.linkText("Finding Dory")).click();
with this one:
driver.findElement(
By.xpath(
"//div[contains(.,'Finding Dory')]/following-sibling::a[contains(.,'Play trailer')]"
)).click();
You can try the following code
driver.findElement(By.xpath(.//a[contains(text(),'Finding Dory'))].click();
Note: If "Finding Dory" is having any link then only the above code will work.

WebDriver exception after SoapUI project execution

Please help to resolve the below issue.
I am using the Eclipse.My main class call the 2 below class/java file.
1) Invoke_SoapUI_Project.java ( this executes the SOAP UI project )
2) Run_Selenium_Script.java ( This opens one of the URL from Firefox ).
My main function call the above Invoke_SoapUI_Project.java and triggers the execution of "SOAP UI XML Project" and it runs well.
Then my second function "Run_Selenium_Script.java" calls and it tries to open the one of Webpage. but getting the below error at the line where i am defining webdriver object. i.e driver= new firefoxDriver();
But if i comment the Invoke_SoapUI_Project.java, then i won't get below exceptions, the firefox object creates properly and it opens the my URL.
The below exception i am getting:
org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: '2.44.0', revision: '76d78cf323ce037c5f92db6c1bba601c2ac43ad8', time: '2014-10-23 13:11:40'
System info: host: 'BDC8-L-HP26ZR1', ip: '127.0.0.1', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_31'
Driver info: driver.version: FirefoxDriver
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:593)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:126)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:191)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:186)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:182)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:99)
at Services.CommonFunctions.setUp(CommonFunctions.java:1481)
at Services.CommonFunctions.obj_run(CommonFunctions.java:1631)
at Services.CSS_Validation_a.print(CSS_Validation_a.java:283)
at Services.CSS_main.main(CSS_main.java:67)
Caused by: java.lang.NullPointerException
at org.apache.http.impl.conn.SystemDefaultRoutePlanner.determineProxy(SystemDefaultRoutePlanner.java:79)
at org.apache.http.impl.conn.DefaultRoutePlanner.determineRoute(DefaultRoutePlanner.java:76)
at org.apache.http.impl.client.InternalHttpClient.determineRoute(InternalHttpClient.java:124)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:183)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at org.openqa.selenium.remote.HttpCommandExecutor.fallBackExecute(HttpCommandExecutor.java:215)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184)
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.execute(NewProfileExtensionConnection.java:165)
at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:362)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572)
... 10 more
We stumbled across what appears to be the same problem here (took us quite some time) but we managed to work around it:
The Problem seems to be that SoapUI sets the default proxyselector to null and Apache httpclient is not expecting this.
Reproducing the Problem:
WsdlProject wsdlProject = new WsdlProject("");
WebDriver driver = new FirefoxDriver();
Workaround:
ProxySelector proxy = ProxySelector.getDefault();
WsdlProject wsdlProject = new WsdlProject("");
ProxySelector.setDefault(proxy);
WebDriver driver = new FirefoxDriver();
peidong-hu sent a patch for standalone two days ago (took the solution from there):
https://github.com/Ardesco/selenium-standalone-server-plugin/issues/23
I already filed an error report for selenium, will update it with this details: https://github.com/SeleniumHQ/selenium/issues/388
I'm also going to file a report for SoapUI.
As per the link
You need to set the firefox driver and provide it as system property.
For eg:
System.setProperty("webdriver.firefox.driver", "c:/webdriver/firefoxdriverpath");
I also found this issue when using SoapUI 5. I checked the source code.
In the class ProxyUtils, the method setGlobalProxy() will set the proxySelector to null if you don't set the proxy or set the proxy to Automatic.
public static void setGlobalProxy( Settings settings )
{
ProxySelector proxySelector = null;
ProxySettingsAuthenticator authenticator = null;
if( proxyEnabled )
{
if( autoProxy )
{
proxySelector = new ProxyVoleUtil().createAutoProxySearch().getProxySelector();
}
else
{
proxySelector = getManualProxySelector( settings );
}
if( proxySelector != null )
{
// Don't register any proxies for other schemes
proxySelector = filterHttpHttpsProxy( proxySelector );
}
authenticator = new ProxySettingsAuthenticator();
}
*ProxySelector.setDefault( proxySelector );*
Authenticator.setDefault( authenticator );
HttpClientSupport.setProxySelector( proxySelector );
HttpClientSupport.getHttpClient().setCredentialsProvider( getProxyCredentials( settings ) );
}
In Windows OS, proxySelector = new ProxyVoleUtil().createAutoProxySearch().getProxySelector(); also will be null.
if (PlatformUtil.getCurrentPlattform() != PlatformUtil.Platform.WIN) {
proxySearch.addStrategy(ProxySearch.Strategy.BROWSER);
// For Windows both BROWSER and OS_DEFAULT will end up with an IEProxySearchStrategy.
// The call in createPacSelector to winHttpDetectAutoProxyConfigUrl is quite slow and we don't want to do it twice.
}
My solution is modify the ProxyUtils class. Change ProxySelector.setDefault( proxySelector ); to
if(proxySelector != null){
ProxySelector.setDefault(proxySelector);
}
else{
proxySelector = ProxySelector.getDefault();
}
Then I use my own ProxyUtils.class, update the ProxyUtils.class in soapui-xxx.jar (in SOAPUI_HOME\bin\) through WinRAR or 7Z. Now my SoapUI works very well.

org.openqa.selenium.NoSuchElementException: Unable to locate element:

Code:
public void Test2() throws Exception{
Thread.sleep(5000);
driver.findElement(By.id("cboMenu")).click();
driver.findElement(By.xpath(".//*[#id='cboMenu/option[3]")).click();
}
Error:
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"cboMenu"}
Command duration or timeout: 31 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:03'
System info: host: 'venu-PC', ip: '192.168.1.3', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_51'
Session ID: 0f859bed-35df-4eba-a472-3bc2efec4814
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Please use explicit wait instead of the Thread.sleep(5000), like in the next example.
It will provide you much clearer error regarding what you experience.
public void Test2() throws Exception{
new WebDriverWait(driver, 3).until(ExpectedConditions.visibilityOfElementLocated(By.id("cboMenu")))
driver.findElement(By.id("cboMenu")).click();
driver.findElement(By.xpath(".//*[#id='cboMenu/option[3]")).click();
}
Next, verify your button doesn't appear in different iFrame. If you do, change the iFrame to the one you element inside:
driver.switchTo().frame("IFRAME_ID");
The IFRAME_ID is taken from the DOM:
<iframe id="IFRAME_ID">
You can next change visibilityOfElementLocated to presenceOfElementLocated, that will verify that an element is present on the DOM but does not necessarily mean that the element is visible. It can be good clue to know if your webDriver is in the correct scope with the button you try to click.
Additional tip - scroll the button you want to click on into view. Thats could be also the reason to failure.
//element is WebElement
(JavascriptExecutor)driver.executeScript("arguments[0].scrollIntoView(true);", element);
this is solved my issue :)
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.context("WEBVIEW_com.openstream.cueme.services.workbench");
Thread.sleep(10000);
driver.findElementById("userId").sendKeys("sysadmin");
driver.findElementById("CuemePassword").sendKeys("MMNext13#");
Try below code
public void Test2() throws Exception{
Thread.sleep(5000);
driver.findElement(By.id("cboMenu")).click();
driver.findElement(By.xpath(".//*[#id='cboMenu']/option[3]")).click();

Categories

Resources