What command do I use to fix this problem? - java

My main goal is to create a program that can automate an android app to do everything you would with your fingers.
I wrote this line of code so that if that switch is turned ON, the code will switch it OFF.
driver.findElement(By.id("com.offerup:id/shipping_checkbox")).click();
But when I run my code again, that switch is now OFF by default.
I need a command that leaves the switch alone if it is already OFF.
Does anyone know what command to use?

You need to check whether the switch is ON or not. You could achieve this using the following code:
if(driver.findElement(By.id("com.offerup:id/shipping_checkbox")).isSelected()){
driver.findElement(By.id("com.offerup:id/shipping_checkbox")).click();
}
Updated:
As mentioned by Muzammil, isSelected() is not working as expected. Alternatively, you could use the checked attribute:
MobileElement shippingCheckbox= driver.findElement(By.id("com.offerup:id/shipping_checkbox"));
if(shippingCheckbox.getAttribute("checked").equalsIgnoreCase("true")){
shippingCheckbox.click();
}

There are 2 ways to check slider is on or off.
This is Node details of Slider.
Option-1: By using text
Here we will click only if slider is On.
MobileElement sliderElement=driver.findElement(By.id("com.offerup:id/shipping_checkbox"));
String sliderStatus = sliderElement.getText();
if (sliderStatus.equalsIgnoreCase("On")) {
sliderElement.click();
}
Option-2: By using Attribute
Here we will click only if slider is On.
MobileElement sliderElement=driver.findElement(By.id("com.offerup:id/shipping_checkbox"));
String sliderStatus=sliderElement.getAttribute("checked");
if (sliderStatus.equalsIgnoreCase("true")) {
sliderElement.click();
}
}

Edit: I made a mistake in reading this, and missed that it was an Android app, so Javascript is likely unavailable. This comment will only help someone with the same issue on web browser testing, or any situation where Javascript can be executed on the application.
Additional possibility for checking. Not that it is superior to other options.
using OpenQA.Selenium.Support.Extensions;
string selector = "com.offerup:id/shipping_checkbox";
if(driver.ExecuteJavascript<bool>($"return document.getElementById('{selector}').checked")) {
driver.findElement(By.id($"{selector}")).click();
}

Related

Selenium Assert questions - Java

How are you?
I'm starting with selenium, and I'm trying hard to deal with "AssertTrue (boolean condition)", but there's something wrong.
I need the Junit gets green(ok) if I access successfully one page, and I want it get red(nok) if it's not possible to access.
But all I got is Green, every single time!
For example:
If I access the payment page it's ok (I assert that there's a buy button to be sure is the payment page)
If I try to access without being logged then the system displays an error message (If I do not find that buy button I won't be in the payment page)
So here's what I'm trying to do in the end of my public boolean method:
if(buybutton.isDisplayed()){
return true;
} else {
return false;
}
So in my test class I'm trying to assert:
Shoes is what I'm buying, and the Shoeslink.com is where I can find it. I put this two variables there because there's an IF - if the user wants to buy shoes selenium will expect some elements, if is a shirt selenium will expect others. But I guess it doesn't matter, because the assert is defined by the code I put above, right? And below is the assertcode I'm talking about:
assertTrue(buy.AccessProduct("**Shoes**", "**Shoeslink.com**"));
Well, if you guys have another logic or way to do it, that's all I need by now: A successfull test if I find an element or an error test if I don't.
Thank you!
I think you wrongly wrote parameters of assertTrue:
assertTrue("Error Message in case of false", buy.AccessProduct("**Shoes**", "**Shoeslink.com**"));

Tapestry5 Ajaxformloop limit number of rows

I am trying to limit the number of rows that a user can add in an ajaxformloop.
Short example:
For example, the loop found in the tapestry 5 documentation here: http://tapestry.apache.org/5.3/apidocs/org/apache/tapestry5/corelib/components/AjaxFormLoop.html
If for example I would only like the user to be able to enter 3 phone numbers, how can that be done?
What I have tried:
1) I tried returning null from the onAddRow event, this causes an exception and the exception report page to display - these events shouldn't return null I don't think.
2) I tried adding my own add row button like this:
<p:addRow>
<t:addrowlink>Add another</t:addrowlink>
</p:addRow>
And then putting a t:if around it, like this:
<t:if test="canAddMorePhones()">
<p:addRow>
<t:addrowlink>Add another</t:addrowlink>
</p:addRow>
</t:if>
In this case, the "add another" reverts to the default "Add row" button and my add row link doesn't show.
3)I tried moving that t:if inside the , this had similar results.
--------------------------
I am sure that this is a fairly common aim, is there any simple way to do it? Perhaps someone can provide an example, and if possible this can help to go in the documentation as i'm sure i'm not going to be the only one trying to do this.
Note: I did also ask on the T5 users mailing list and had one answer but I can't seem to get it working after the response from Lance (Which I am sure is probably correct, but i'm not sure how to use the AjaxResponseRenderer as per my reply last week, this is probably due to my own technical limitations or understanding of some parts of T5).
http://apache-tapestry-mailing-list-archives.1045711.n5.nabble.com/Ajaxformloop-add-row-link-max-size-tt5730840.html
I also tried using ajaxResponseRenderer.addRender as you did in your mailing list code, but it doesn't work because it seems that Tapestry has some problems dealing with updating a component that's busy updating another component. However, AjaxResponseRenderer also supports execution of JavaScript. Taking this approach on the AjaxFormLoop example in the docs, specify the addrowlink as follows:
<p:addrow>
<t:if test="canAddMorePhones()">
<t:addrowlink id="addRowLink" t:id="addRowLink">Add another</t:addrowlink>
</t:if>
</p:addrow>
Then add the following code right before return phone; in onAddRowFromPhones():
ajaxResponseRenderer.addCallback(new JavaScriptCallback() {
public void run(JavaScriptSupport javascriptSupport) {
if (!canAddMorePhones()) {
javascriptSupport.addScript("document.getElementById('addRowLink').style.display = 'none';");
}
}
});
This example was tested successfully in Tapestry 5.3.7.

Selenium IEDriverServer not sending keys when using RemoteWebDriver

So I have a test case set up which works perfectly fine using Testng and running the IEDriverServer locally. But when running the test case using Grid 2 the following command doesn't appear to work:
driver.findElement(By.xpath("(//input[#type='text'])[3]")).sendKeys(logNum);
There are no errors and the output from the node states that it had completed, but no text appears in the edit box. I checked running the test through a debugger and the "logNum" variable does have a value
I can not fathom why its not working
I am using selenium-server-standalone-2.44.0.jar running the Hub and Node on the same machine
I have resolved this by removing
capability.setCapability("nativeEvents", false);
from my initiation of the driver.
I recently came across the same problem. I used JavascriptExecutor to set the value of the element.
public static void useJSSendKeys(String value,WebElement element){
JavascriptExecutor myExecutor = ((JavascriptExecutor) driver);
myExecutor.executeScript("arguments[0].value='"+value+"';", element);
}
good to know that your issue is resolved on making capability.setCapability("nativeEvents", false); but it may create problem when you try to perform native events. so if you not tried, please try performing click before sendkeys. it helps me some times and also providing Thread.sleep(3000) in java also helps me in some situations. please try these if not.
Thank You,
Murali

How can I detect a close in the browser tab?

Hello I am looking for information on the close tab (not browser) event if there is one in java for a applet. I am wondering if there is an event for that or a way to check a way to check for that. I would like to just capture the event and make a little popup box , stating Your session will expire or something along those lines. Is that possible at all or to a point with java or Javascript?
UPDATE: okay with the information you guys pointed me into i was able to get information on a simple enough javascript. Now it is working fine in IE , Chrome and Firefox but for some reason Safari 5.1.7 isn't liking the code. Not sure why. Here is the code if it helps.
jQuery(function() {
var hiddenBtn = document.getElementById("javaform:browserCloseSubmit");
try{
opera.setOverrideHistoryNavigationMode('compatible');
history.navigationMode = 'compatible';
}catch(e){}
//Sends the information to the javaBean.java file.
function ReturnMessage()
{
return hiddenBtn.click();
}
//UnBind Function
function UnBindWindow()
{
jQuery(window).unbind('beforeunload', ReturnMessage);
}
//Bind Exit Message Dialogue
jQuery(window).bind('beforeunload', ReturnMessage);
});
You have the onBeforeUnload event you can catch in JavaScript. See here.
Use window.onbeforeunload
window.onbeforeunload = function () {
return "Are you sure you want to exit?";
};
Note that it will also end in Are you sure you want to leave this page (or are you sure you want to reload this page if you are reloading)

Open and maximise a browser window in OATS Java (not javascript)

I am a tester and just installed oracle application test suite to use testing eBus apps
Anyway the only language it supports for coding test scripts (I don't want to use the recorder for a number of reasons). The problem I am having is that everything I search or google is javascript not java (even googling with -script I still ended up looking at javascript. This just gets rejected by the oats editor
The only other examples I have seen, appear to be defining a variable then setting the value of that variable as the window they want to maximize. Aside from the fact that my java skills are not up to doing that - I do not need to do this for a newly opened browser window do I? (The assumption is that this will be the only browser window open (ie test is executed with browser closed)
Is there any easy way to do this?
Below is the very simple initiate of the browser which is generated from a recording plus part of the first step which loads the url the test starts at: (I realize the first step is not complete below -I didn't paste it all, just enough to hopefully allow someone to show me what I need to edit to force the browser to load maximized, or maximize it immediately after loading?
public void initialize() throws Exception {
browser.launch();
}
/**
* Add code to be executed each iteration for this virtual user.
*/
public void run() throws Exception {
beginStep("[1] Login (/RF.jsp)", 0);
{
web
.window(2,
"/web:window[#index='0' or #title='about:blank']")
.navigate(
"http://somepageiwantolaunch");
web.window(4, "/web:window[#index='0' or #title='Login']")
.waitForPage(null);
I am not sure whether you already got the answer for this.. if not this code should help you
browser.launch();
DOMBrowser currentExecutionBrowser = web.window("/web:window[#index='0' or #index='1']");
currentExecutionBrowser.maximize();
Let me know if this helps!
There is a function in the Oracle Functional Tester API Reference which has a build in function called object.WindowState It says you can get or set using this function and it has values
0 - Normal, 1- minimized and 2-maximised.
Only issue is that these examples look more like VB than Javascript but presumably there is a similar function built into to the Oracle libraries for Java.
I did a quick search for Oracle Openscript API and came up with this link which asks for the same thing. They suggest using Help->Search from within the openscript application and then searching for "openscript API" which should provide a list of the functions available.
Hope that helps.
To Maximize browser in OATS, follow the below code
Open script ha in built methods which helps coding easy
browser.launch();
web.window(12, "/web:window[#index='0' or #title='about:blank']").navigate("http://www.google.com/");
web.window(12, "/web:window[#index='0' or #title='about:blank']").maximize();
for more OATS Tips/Tricks follow here
http://www.testinghive.com/category/oracle-application-testing-suite-tips
If it is the only browser window open, you can use the below code. It must be used with caution since the code maximizes any window that is open above the browser window.
try {
Robot a = new Robot();
a.keyPress(KeyEvent.VK_ALT);
a.keyPress(KeyEvent.VK_SPACE);
a.keyRelease(KeyEvent.VK_SPACE);
a.keyRelease(KeyEvent.VK_ALT);
a.keyPress(KeyEvent.VK_X);
a.keyRelease(KeyEvent.VK_X);
} catch (AWTException e) {
}

Categories