How to check link is already clicked in java? - selenium

I am new to Selenium and also new to java coding.
Can you please tell me coding for already selected or active link of table and print data of table for already selected link then go to next link automatically?

I would recommend you the following approach.
Algorithm is simple:
1) if a link has certain color attributed -> then it is supposed to be not clicked yet
2) otherwise ( if color attribute differs) -> it has been clicked
So you should find selector of the link to analyze:
String cssLink ="blalbalbla";
then you find this element (link):
WebElement link1 = driver.findElement(By.cssSelector(cssLink));
then you get color attribute of it:
String colorExtracted= link1.getAttribute('color');
then you can analyze color obtained:
boolean result;
if(colorExtracted==clicked) {
result = true;
}
else {
result=false;
}
Hope this idea helps you.

Related

Selenium webdriver: How to check an img element really have height attribute

in java Selenium web driver on chrome, I need to make sure that an image has a height attribute. The HTML code I have is this:
<img src="https://www.w3schools.com/tags/img_girl.jpg">
and I'm trying to execute
boolean s = driver.findElement(By.tagName("img")).getAttribute("height") !=null;
but this always returns true although there's no height attribute! and if I try to execute
String s = driver.findElement(By.tagName("img")).getAttribute("height");
I'll get 600 despite there is no height attribute in the HTML code. is there any other way I can check it?
Try this example:
#Test
public void testHeightAttribute(){
driver.get("https://webelement.click/en/welcome");
WebElement img = driver.findElement(By.xpath("//img[1]"));
System.out.println(img.findElements(By.xpath(".[#height]")).size());
System.out.println(img.findElements(By.xpath(".[#src]")).size());
}
I am not sure this is the most elegant way though :). The idea is that you're looking up elements using the element itself as a search context (looking up element within itself). If no attribute is present in DOM then the list would be empty (size() == 0). Otherwise it would be 1.
Basically the common method would look like:
public boolean hasAttributeInDom(WebElement element, String attr){
return !element.findElements(By.xpath(".[#" + attr + "]")).isEmpty();
}
You can check height parameter in styles section of Developer tools. Refer to attached screen shot of sample image from Stack overflow. You can see height parameter at right hand side. Generally, dimension are mentioned here.
You will need to understand and look for particular image property in Styles section as its display CSS properties for entire page.

How can I get a click on a row in a table in Cuba framework?

I have to detect a click in a row in a table in Cuba framework but I don't find how.
I have a TreeTable. Then I have a button. If I have nothing selected in the table, I want the button disabled. If I click on a item I want the button to be enabled. If I click on a sub-item, I want the button to be disabled.
It is possible, in the action of the button to use a:
trackSelection = true
That will work, but enabled the button too if I click on a sub-item.
Then The idea is, when anything is clicked on the table, then do something. I have only to track the selection of any item, and then do a logic.
How can I track this selection?
I have tried anything like:
table.setClickListener("columnId", new CellClickListener() {
#Override
public void onClick(Entity item, String columnId) {
// TODO Auto-generated method stub
LOG.info("On cell click");
}
});
First, that don't work, even if I click on a cell. Then even if it will work, I have the complete row and not only a cell.
Somebody have an idea?
Thanks
Best regards
You can react on selection change in Table using CollectionDatasource.ItemChangeListener:
employeesDs.addItemChangeListener(event -> {
log.info("Datasource {} item has been changed from {} to {}",
event.getDs(), event.getPrevItem(), event.getItem());
});
See also: https://doc.cuba-platform.com/manual-6.9/datasource_listeners.html

Screenscraping a Datepicker with Scrapy + Selenium

So i need to scrap a page like this for example or this and i am using Scrapy + Seleninum to interact with a datepicker calendar.
Basically i need to check for the availabilities of the rooms for each day in a month for example, so my idea was trying to click the not disabled links/dates on the datepicker and check if an error message appears or not for me to know if its available or not.
I have no idea if this is the proper way or even how to do this with this tools. I have been playing around with them but still looks like i am far from a final solution.
Does anyone know how i can tackle this or even provide the code for me to achieve this?
Hi please find the answer below
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://www.airbnb.pt/rooms/516301?check_in=2016-04-14&guests=1&check_out=2016-04-17");
// selecting firstdate picker -- check in
driver.findElement(By.xpath("//*[#class='col-sm-6']/input")).click();
// note calendar is not completely visible hence to make it visible
// scroll a little bit down
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,200)");
// take all calendar dates inside the list
// here i have update my code to skip stale element exception
List<WebElement> alldates = driver.findElements(By.xpath("//*[#class='ui-datepicker-calendar']/tbody/tr/td/a[contains(#class, 'ui-state-default')]"));
System.out.println("Size is : "+ alldates.size());
// suppose you want to select 27 form the all dates
// keeping it as a parameter
String dateToBeSelected = "19"; // check in
for(int i=0;i<alldates.size();i++){
alldates = driver.findElements(By.xpath("//*[#class='ui-state-default']"));
System.out.println("dates is : " + alldates.get(i).getText());
// selects a check-in date
if( alldates.get(i).getText().equals(dateToBeSelected)){
alldates.get(i).click();
break;
}
}
// on selection of Checkin date check out calender automatically pop ups
System.out.println("--------------------------");
String datetobeselected = "27";
for(int i=0;i<alldates.size();i++){
alldates = driver.findElements(By.xpath("//*[#class='ui-state-default']"));
System.out.println("dates is : " + alldates.get(i).getText());
// selects a check-in date
if( alldates.get(i).getText().equals(datetobeselected)){
alldates.get(i).click();
break;
}
}
// actually after calendar selection whatever text is shown in red color
// i am not sure what is it (sorry only English)
// identify the text with red color is present or not
boolean errorMsg = driver.findElement(By.xpath("//*[#class='panel-body panel-light']/div[2]/p")).isDisplayed();
if(errorMsg){
System.out.println("error msg is displayed");
}else{
System.out.println("error msg is not - displayed");
}
Hope this helps you also note this above example takes check-in and checkout date as parameter if you want code to take non disabled date automatically please feel free to ask i will update my answer.

Create FullPage Screenshot WebDriver

Does someone knows a way to create full page screenshots using WebDriver?
I want if one of my tests fails to create a FULL PAGE (even the not visible part on the screen) screenshot before the browser close and save it on share location.
Also, if it is possible I want to output the result to Jenkins Console log.
Thanks!
You can use the following extension for Firefox: https://addons.mozilla.org/nl/firefox/addon/fireshot/
You can find its javascript code in %APPDATA%\Mozilla\Firefox\Profiles\
The extensions provide the ability to copy the screenshot to the clipboard.
You can use its JS methods to perform the screenshot. After that, you can retrieve the image from the clipboard and save it to as a file on shared location.
Image image = default(Image);
if (Clipboard.GetDataObject() != null)
{
IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.Bitmap))
{
Image image = (Image)data.GetData(DataFormats.Bitmap,true);
image.Save("image.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
Console.WriteLine("The Data In Clipboard is not as image format");
}
}
else
{
Console.WriteLine("The Clipboard was empty");
}
string newImageName = string.Concat(#"C:\SampleSharedFolder\", Guid.NewGuid());
image.Save(newImageName );
Console.WriteLine("Image save location: {0}", newImageName);
Once you have populated the result to Console it is really easy to output it back to Jenkins. You can find more in my article: http://automatetheplanet.com/output-mstest-tests-logs-jenkins-console-log/
You can use Snagit to perform full page screenshots. More information here: https://www.techsmith.com/tutorial-snagit-documentation.html
First you need to start the Snagit server and then follow the documentation.

Titanium: click on element(View, Label e.t.c.) should increase its top by 1

I've binded the same function to all elements that I want(let's say View and Label).
In this onClick handler function I want to get the element which was clicked and increase its 'top' by 1.
Please tell me if you know how to do that, I can't find an answer yet.
My global click handler function:
function increaseElementTop(e) {
// Here we should increase element's top position.
Ti.API.info(JSON.stringify(e));
}
Thanks, any help is appreciated.
Get the e.source attribute from the Event e. Then use the setTop() method.
if (e.source == $.yourLabel) {
$.yourLabel.setTop($.yourLabel.getTop() + 1)
}
UPDATE: Regarding your comment: I thought it would only be used for two elements. A universal approach would be something like this:
e.source.setTop(e.source.getTop() + 1);
Unfortunately I have no machine available to test the code but I will do so later (Tuesday I guess). Just try it and let your programm print the e.source variable. If this does not work you can also try to use e.source.getId().
function increaseElementTop(e) {
var theUIElement = e.source;
theUIElement.top = theUIElement.top + 1;
}
Add the event listener to each of your UIElements, and this will get the element you click on & modify the top.