Verification in Selenium webdriver - selenium-webdriver

I am new to the automation world, I am trying to verfy an item in the list to see if it has been deleted or not.. I have used the following syntax but it says company found, which is not suppose to be the answer.. Please help
internal void verifycompany()
{
Thread.Sleep(1000);
int b = 1;
bool CompanyName = Driver.driver.FindElement(By.XPath(".//*[#id='companies']/tr[" + b + "]/td[1]")).Displayed;
if (CompanyName == false)
{
Console.WriteLine("company not found, test succesful");
Base.test.Log(LogStatus.Info, "company not found, test succesful");
}
else
{
b++;
Base.test.Log(LogStatus.Info, "Company found, test unscuccesful");
}
}

Unfortunately, isDisplayed does not return false if it is not displayed, it throws an exception stating that it couldn't be found (geez thanks).
What I did is create a reusable method where I can pass in an element and it'll throw it through a try catch and return true if found, or false if the exception is thrown.
Something like the below - I use java but it should help you figure out a way around your problem:
public boolean isDisplayed(WebElement webElement) {
try {
return element.isDisplayed();
} catch (NoSuchElementException e) {
return false;
}
}
If you don't want to create a reusable method, just throw your find element line into a try catch.
boolean CompanyName;
try {
CompanyName = findElementAndIsDisplayedCode;
} catch (NoSuchElementException e) {
CompanyName = false;
}

Related

MSAL4j - How to handle MsalThrottlingException?

I use MSAL4j and there is an exception type named MsalThrottlingException. How can I handle it when I catch it? I need an example implementation.
try{
Future<IAuthenticationResult> future =
confidentialClientApplication.acquireToken(authorizationCodeParameters);
IAuthenticationResult authenticationResult = future.get(1, TimeUnit.MINUTES);
}
catch(ExecutionException e){
if(e.getCause() instanceof MsalThrottlingException){
//how to handle it
}
}
https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-error-handling-java
There was a document about it also(you can see the screen shot in above link), but it doesn't give a example handling implementation. Could you give an example?
This worked for me:
private static String PREFIX_RETRY_STR = "com.microsoft.aad.msal4j.MsalThrottlingException: Request was throttled according to instructions from STS. Retry in ";
private static String SUFFIX_RETRY_STR = " ms.";
(...)
if (e.getCause() instanceof MsalThrottlingException) {
int waitTime = Integer.parseInt(e.getMessage().replace(PREFIX_RETRY_STR, "").replace(SUFFIX_RETRY_STR, ""));
try {
TimeUnit.MILLISECONDS.sleep(waitTime);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
result = pca.acquireToken(parameters).join();
} else
throw e;

How to commit programmatically via a managed bean?

I have a managed bean that changes an attribute value and calls a popup.
I need also to commit the changes made (without having to click a commit button), I tried some code but it does nothing.
Help me, please.
DCBindingContainer bindings = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
OperationBinding operationBinding = bindings.getOperationBinding("Commit");
operationBinding.execute();
You can use the following functions to commit the change made to an Iterator programmatically (In an action listener for example) :
public static ViewObjectImpl getViewObjectFromIterator(String nomIterator) {
ViewObjectImpl returnVO = null;
DCBindingContainer dcb = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
if (dcb != null) {
DCIteratorBinding iter = dcb.findIteratorBinding(nomIterator);
if (iter != null) {
returnVO = (ViewObjectImpl)iter.getViewObject();
}
}
return returnVO;
}
private void commit(String IteratorName) {
ViewObject vo = this.getViewObjectFromIterator(IteratorName);
try {
vo.getApplicationModule().getTransaction().validate();
vo.getApplicationModule().getTransaction().commit();
} catch (ValidationException e) {
String validationErrorMessage = e.getDetailMessage();
//Occur when some committed data is rejected due to validation error.
//log it : log(Level.WARNING, " " + validationErrorMessage);
}
catch (Exception e) {
//Log it and warn something unexpected occured
}
}
//In your action listener simply call the commit function as follow
//You can Find YOUR_ITERATOR_NAME the your PageDef Binding file in the Executables Column
commit("YOUR_ITERATOR_NAME");
See more : https://gist.github.com/CedricL46/04570c1f078583321ad680ee8ba28f72

How to print text below and above Passed: in the console area in selenium

I have three #test methods to add, update and delete records in a webpage, once my execution completes I am looking for an output something like below, where can I use the sisout to print the highlighted messages, I am running my code in TestNG.
#Test(priority=2)
void updateAddressBook() {
try{
driver.findElement(By.id("radio1")).click();
driver.findElement(By.id("edit")).click();
driver.findElement(By.id("company")).clear();
Thread.sleep(3000);
driver.findElement(By.id("company")).sendKeys("Vuram");
driver.findElement(By.id("add")).click();
Log.info("Validate company name is updated for user 2");
Reporter.log("Company name sucessfully updated for user 2");
passCount++;
} catch (Error e) {
verificationErrors.append("Cannot provide the input please stick to the UI constraints5.\n");
} catch(Exception e){
verificationErrors.append("Cannot provide the input please stick to the UI constraints6.\n");
}
try {
String x = driver.findElement(By.id("result")).getText();
if(x.contains("Vuram"));
{
passCount++;
}
} catch (Error e) {
verificationErrors.append("Element by id 'td' not found.\n");
} catch (Exception e) {
verificationErrors.append("Element by id 'td' not found.\n");
}
}
This is the solution what are you looking for, Just implement it sample demo:
System.out.println(); for Console Logs
#Test(description = "") for Test Description
public class demo {
#Test(description = "This is description of Test Case")
public void TestDemo() {
System.out.println("****************************");
System.out.println("Hello World");
System.out.println("****************************");
}
}

Method 'getValue' to get the value of a checkbox/radioButton/textBox in webdriver

I am implementing a method 'getValue' (return type needed is String) in java to get the value of a checkbox/radioButton/textBox using webdriver as below:
try {
element=driver.findElement(By.xpath(target));
BoolResult = element.isSelected();
if(BoolResult==true) {
result="Radio Button/textbox Selected";
return result;
} else if(BoolResult==false){
result="Radio Button/textbox Not Selected";
return result;
} else {
}
} catch(Exception e2) {
}
try {
element=driver.findElement(By.xpath(target));
result = element.getAttribute("value");
log.info("The value of the target is : " + result);
} catch (Exception e) {
log.debug(e);
return result;
}
The above is working fine for Radio Button, but for TextBox, its going to the BoolResult==false condition and saying that 'RadioButton/TextBox Not selected', whereas i am expecting it to go to the next try block and execute the 'element.getAttribute("value")'.
Can someone please help here?
You need to determine whether the element is a checkbox or textbox. You should use more specific xpaths so that you know what you're dealing with before getting to this point, but anyways, this should work (it's untested though):
element=driver.findElement(By.xpath(target));
string type = element.getAttribute("type");
if (type.equals("checkbox") {
if(element.isSelected()) {
result="Radio Button/textbox Selected";
} else {
result="Radio Button/textbox Not Selected";
}
else if (type.equals("text")) {
result = element.getAttribute("value");
}
else {
log.debug("Unexpected input type");
}
return result;

Missing directive or assembly reference for SoapException

I'm using the salesforce API and have a helper class that returns a SaveResult array. I'm using a try/catch block and I'm using SoapException in my catch block, but the namespace is not found. I'm not sure what using directive I should use?
The examples in the API guide show SoapException as a valid type.
Here is my method:
private SaveResult[] CreateObjects(sObject[] objectArray)
{
try
{
SaveResult[] saveResults = this.binding.create(objectArray);
for (int i = 0; i < saveResults.Length; i++)
{
if (saveResults[i].success)
{
Console.WriteLine("An object was created with Id: {0}", saveResults[i].id);
}
else
{
Console.WriteLine("Item {0} had an error updating", i);
foreach (Error error in saveResults[i].errors)
{
Console.WriteLine("Error code is: {0}",
error.statusCode.ToString());
Console.WriteLine("Error message: {0}", error.message);
}
}
}
return saveResults;
}
catch (SoapException e)
{
Console.WriteLine(e.Code);
Console.WriteLine(e.Message);
}
return null;
}
Any help is appreciated.
Thanks.
Its the library provided SoapException class, see http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapexception.aspx

Resources