How to out while loop when button is disabled? - java

I used WebDriver sampler and wrote a Selenium Java script- In below script
var x = 0
var nextt = WDS.browser.findElement(pkg.By.xpath("//button[contains(#class,'btn btn-sm btn-light border ml-3')]"))
var nextisEnb = nextt.isEnabled()
while (true) {
if (nextisEnb == true) {
//if(next.isEnabled()){
++x
//java.lang.Thread.sleep(2000);
var wait9 = new org.openqa.selenium.support.ui.WebDriverWait(WDS.browser, 9000)
wait9.until(org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable(pkg.By.xpath("//button[contains(#class,'btn btn-sm btn-light border ml-3')]")))
nextt.click();
WDS.log.info('btn50-clicked ' + x + 'isEnabled=' + nextisEnb)
//break
if (nextisEnb == false) {
break
}
}
}
When nextt button is disabled still it not break the loop . it still running in Jmeter. So how we can out for the loop?
I also used below code got same problem
var next = WDS.browser.findElement(pkg.By.xpath("//button[contains(#class,'btn btn-sm btn-light border ml-3')]"))
while (next.isEnabled()) {
java.lang.Thread.sleep(2000);
var wait9 = new org.openqa.selenium.support.ui.WebDriverWait(WDS.browser, 9000)
wait9.until(org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable(pkg.By.xpath("//button[contains(#class,'btn btn-sm btn-light border ml-3')]")))
next.click();
WDS.log.info('btn50-400clicked')
}
WDS.log.info('btn2clicked')
//java.lang.Thread.sleep(3000);
if (!next.isEnabled()) {
WDS.log.info('Next button disabled')
}
How is it resolved?

You're always querying the initial state of the "Next" button, you need to "find" it once again to get the "fresh" status after clicking. My expectation is that when you click the "Next" button the DOM gets refreshed and the button has new properties and you have stored its previous state into a global variable.
next.click()
WDS.log.info('btn50-400clicked')
//here the page has refreshed and you need to "find" the button once again
next = WDS.browser.findElement(pkg.By.xpath("//button[contains(#class,'btn btn-sm btn-light border ml-3')]"))
if (!next().isEnabled() {
break
}
If anything goes wrong you can always take a screenshot like:
var screenshot = WDS.browser.getScreenshotAs(pkg.OutputType.FILE)
screenshot.renameTo(new java.io.File('screenshot.png'))
or print the current HTML source code of the page to JMeter.log file like:
WDS.log.info(WDS.browser.getPageSource())
to check the button state either visually or by inspecting its HTML definition.
More information: The WebDriver Sampler: Your Top 10 Questions Answered

Related

How to disable apex command button based on a checkbox being checked or unchecked?

I'm creating a visualforce page for my company and I want the submit button to be disabled unless a checkbox on the page is changed. I was able to do this with a regular input button but I need this to be done with an Apex command button so I can have them update the record which will fire a back-end process.
Here is my code
<apex:page standardController="RMA__c">
<apex:form >
<apex:outputPanel id="thePanel" rendered="{!(RMA__c.RLI_has_QUOTE_SO__c == true)}">
<apex:pageBlock id="TheBlock" title="Confirm Information" mode="edit">
<apex:pageBlockSection id="theSection" title="Confirm the new field values" columns="2" rendered="{!(RMA__c.Show_the_box__c == true)}">
<apex:outputfield value="{!RMA__c.Contact__c}"/>
<apex:outputfield value="{!RMA__c.Shipping_Priority__c}"/>
<apex:outputfield value="{!RMA__c.Ship_to_Address__c}"/>
<apex:outputfield value="{!RMA__c.Bill_to_Address__c}"/>
<apex:inputfield value="{!RMA__c.Request_Priority__c}"/>
<apex:inputField id="reviewed" value="{!RMA__c.Changes_are_reviewed__c}"/>
</apex:pageBlockSection>
<apex:pageblockButtons id="button">
<apex:commandButton value="Submit" action="{!save}" id="saveit" disabled="true"/>
</apex:pageblockButtons>
<script>
var checker = document.getElementById("{!$Component.TheBlock.theSection.reviewed}");
var sendbtn = document.getElementById("{!$Component.TheBlock.button.saveit}");
checker.onchange = function()
{
if(this.checked)
{
sendbtn.disabled = false;
}
else
{
sendbtn.disabled = true;
}
}
</script>
</apex:pageBlock>
</apex:outputPanel>
</apex:form>
You can use reRender property in RMA__c.Changes_are_reviewed_c inputfield
First, wrap command button with <apex:outputpanel>
<apex:outputpanel>
<apex:commandButton value="Submit" action="{!save}" id="saveit" disabled="{!RMA__c.Changes_are_reviewed__c==false}"/>
</apex:outputpanel>
then, rerender it from
<apex:inputField id="reviewed" value="{!RMA__c.Changes_are_reviewed__c}" reRender="saveit"/>
Hope, it will help. I have not compiled the code in salesforce, there may have some compilation error. But, this way, it will work.

Unable to check state of elements

I am trying to automate for my first time a complete flow for a e-comm app. After Add to cart button is clicked, a quantity spinner is displayed instead of that button, with "+" and "-" buttons. I am trying to determine if the button was clicked. If so, click on "-" button and then click again on Add to Cart button for that element and move to next button and so on. This is what I have done so for, but without any luck:
private static void addProductsToCart()
{
List<WebElement> buttons = driver.findElements(By.className("actions__addToCart"));
WebElement action = driver.findElement(By.xpath("//*[#id=\"content\"]/div/section/ul/div/div/li/div/div/div/div/div/button[1]"));
for(int i = 0; i <= buttons.size(); i++)
{
if(action != null && action.isDisplayed())
{
action.click();
}
buttons.get(i).click();
if(i == 4)
{
break;
}
}
}
Any help would be much appreciated.
<div class="quantitySpinner alignmentContainer__element" data-bind="visible: size().quantity() > 0" style="">
<button class="decrease" data-bind="click: decrementQuantity, attr: {'aria-label': ogs.gsa.resources.Accessibility_DecreaseQuantity.formatWith({productBrand: model.Brand, productName: model.Name})}" aria-label="Subtract One Birds Eye Steamfresh Chef's Favorites Rotini & Vegetables"></button>
<label data-bind="text: size().quantity, attr:{'aria-live':'polite', 'aria-atomic':'true'} " aria-live="polite" aria-atomic="true">1</label>
<button class="increase" data-bind="click: incrementQuantity, attr: {'aria-label': ogs.gsa.resources.Accessibility_IncreaseQuantity.formatWith({productBrand: model.Brand, productName: model.Name})}" aria-label="Add One Birds Eye Steamfresh Chef's Favorites Rotini & Vegetables"></button>
</div>
For clicking on decrease button, you can try this code :
WebDriverWait wait = new WebDriverWait(driver , 20);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.decrease"))).click();
As you have mentioned, that you want to click on click again on Add to Cart button
for that :
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath of add to cart button
or any other locator as per your convenience ")))

How to verify the button having drop down menu using selenium in java

There is a button. When you click on this button, a drop down menu having two option appears. How to verify this scenario using selenium in java.
<div class="hpDropDownButton">
<button class="button ng-binding">Holidays Operation</button>
<ul>
<li>
<a class="ng-binding" ng-click="uploadHolidays()">Upload Holidays</a>
</li>
<li>
<a class="ng-binding" ng-click="deleteHolidays()">Delete Holidays</a>
</li>
</ul>
Click on the button
Now :-
Boolean dropdownPresent = driver.findElement("YOUR LOCATOR OF DROPDOWN").isDisplayed();
if(dropdownPresent==true)
{
System.out.println("Dropdown is appearing");
}
else{
System.out.println("Dropdown is not appearing");
}
Hope it will help you :)
You are asking to verify whole scenario. You need to first understand what Selenium-WebDriver is. Refer this tutorial for more explanation.
However you can follow below code,
WebDriver driver = new FirefoxDriver();
String appUrl = "your url";
driver.get(appUrl);
// maximize the browser window
driver.manage().window().maximize();
// upto code from your button
WebElement button_element = driver.findElement(button_locator);
button_element.click();
// to verify a drop down menu having two option appears
boolean flag = isPresent(dropdown_locator);
if (flag) {
// code bases on dropdown displayed
}
else {
// code bases on dropdown not displayed
}
To verify if element is present or not use this method
public boolean isPresent(String locator) {
return findElements(locator).size() > 0 ? true : false;
}
First of all collect all the Drop down values in List, List values = Upload Holidays#Delete Holidays
Then click on Dropdown WebElement, by using DropdownFieldName = driver.findElement(by.xpath(//button[#class='button ng-binding'])).click();
Take the couunt of dropdown values, by drptotalCount = driver.findElements(by.xpath(//a[#class='button ng-binding']));
Now you have expected DropDown values and count of Dropdown Values.
You can take a reference from below code:
checkDropDownValues(String DropdownFieldName, String values){
driver.findElement(By.xath(DropdownFieldName)).click();
WebElement drptotalCount = driver.findElements(by.xpath(//a[#class='button ng-binding']));
int numberOfDropDown = drptotalCount.size();
List<String> allDropDownValues = Arrays.asList(values.split("#"));
for (int colCount = 1; colCount <= numberOfDropDown; colCount++) {
boolean flag = false;
Actualvalue = driver.findElement(By.xpath(drptotalCount + "[.=" + allDropDownValues.get(colCount) +"]"])).getText();
String expectedValues = allDropDownValues.get(colCount);
if(expectedValues.equalIgnoreCase(Actualvalue))
{
flag = true;
}
Assert.assertTrue("Column values doesn't match", flag);
}
}
Click the button (which should be straight forward)
it seems you have some asynchronous call or a delay
Wait for the drop-down 'div.hpDropDownButton' is beeing displayed using WebDriverWait:
WebElement myDynamicDropDown = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.CssSelector("div.myDynamicDropDown")))
continue ..
http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp

i want to open new window browser at every click event... in jsp

I am just facing the issue at open new browser at "each click event previous opened stay that". i want that.
Look i want to open window browser at click event... it opens fine.
But i want at each click it opens new browser. how can i do that?
it always override that new window. i want always open a new window.
i used:
function Validation(){
var i=0;
if(document.netsim.emulatorNo.value=="")
{
alert ( "Please Fiil Emulator Number" );
netsim.emulatorNo.focus();
i=1;
}else {
var emu = document.netsim.emulatorNo.value;
var serverUrl = document.netsim.Apply.value;
window.open('http://localhost:8080/SMSSimulator/NewEmulator.jsp?emulator='+emu+'&ServerUrl='+serverUrl,'mywindow','width=400,height=350');
}
if(i==1)
return false;
}
suggest me to find out my answer.
Thanks in advance.
window.open(url, unique_title, features)
If you want to open it always on the new window use a unique window title everytime, else it will keep on opening on the same window.
Example sample html and popup opens fine in new window always -
<html>
<script>
var counter = 0;
function openWindow(){
window.open('http://www.google.com','mywindow'+counter,'width=400,height=350');
counter++;
}
</script>
<body>
<input type="button" value="button" id="button" onclick="openWindow()" />
</body>
</html>
you need to give different window names to each window. so, 'mywindow' needs to be changed. try something like;
var counter = 0;
function Validation(){
var i=0;
if(document.netsim.emulatorNo.value=="")
{
alert ( "Please Fiil Emulator Number" );
netsim.emulatorNo.focus();
i=1;
}else {
var emu = document.netsim.emulatorNo.value;
var serverUrl = document.netsim.Apply.value;
window.open('http://localhost:8080/SMSSimulator/NewEmulator.jsp?emulator='+emu+'&ServerUrl='+serverUrl,'mywindow'+counter,'width=400,height=350');
counter++;
}
if(i==1)
return false;
}
Here you open the new window in a specific place called 'mywindow'
window.open('http://localhost:8080/SMSSimulator/NewEmulator.jsp?emulator='+emu+'&ServerUrl='+serverUrl,'mywindow','width=400,height=350');
you can change it to blank or "_blank" and it will open it in a new window:
window.open('http://localhost:8080/SMSSimulator/NewEmulator.jsp?emulator='+emu+'&ServerUrl='+serverUrl,'','width=400,height=350');
there is no need to naming it unless you have a javascript that reference the window

Can I trigger an IceFaces action using JavaScript?

If I have a simple button:
<ice:panelGroup>
<ice:commandButton value="foobar"
action="#{fileManager.openNoFlashVisiblePopup}" />
</ice:panelGroup>
Is it possible to trigger the action openNoFlashVisiblePopup using just javascript? I know that there IceFaces has a JavaScript bridge but I don't know see a simple way to do just this.
i need to do this because I have a chunk of JavaScript that detects Flash and I need to show a IceFaces popup.
One way is to get the button element by ID and call its click() function.
document.getElementById('clientId').click();
You only need to give the form and button a fixed id so that you can use the generated HTML ID as clientId in Javascript code.
I know I'm a little late in seeing this, but the correct way to handle this (minus perhaps the overly exhuberant error checking) is:
// There's a <div> that looks like: <div class="portletfaces-bridge-body" id="A8660">.
// We'll find it and pull out the value of the ID to build elementId like: A8660:wtfForm:editeventparent
var div = null;
var divCollection = document.getElementsByTagName("div");
for (var i=0; i<divCollection.length; i++) {
if(divCollection[i].getAttribute("class") == "portletfaces-bridge-body") {
div = divCollection[i];
break;
}
}
if (div == null){
alert("could not find div portletfaces-bridge-body.");
return;
}
// Pull the id out of divInnerText.
var id = div.getAttribute("id");
if (id == null){
alert("id was null");
}
// prepare initializes fields to null so rendered cannot begin until both itemId and parentId are set.
var prepare = document.getElementById(id + ":wtfForm:editeventprepare");
if (prepare == null){
alert("editeventprepare element was not found.");
return;
}
prepare.click();

Categories

Resources