/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.livedata.test.po;

import java.util.Arrays;

import org.openqa.selenium.By;
import org.xwiki.test.ui.po.BaseElement;

/**
 * Live Data page object. Provides the operations to obtain the page objects for the different live data layouts, and to
 * switch between them.
 *
 * @version $Id$
 * @since 13.4RC1
 */
public class LiveDataElement extends BaseElement
{
    // TODO: add the operations to switch between the layouts.

    private final String id;

    /**
     * Default constructor. Initializes a Live Data page object by its id.
     *
     * @param id the live data id
     */
    public LiveDataElement(String id)
    {
        this.id = id;
        waitUntilReady();
    }

    /**
     * @return a table layout page object for the live data
     */
    public TableLayoutElement getTableLayout()
    {
        TableLayoutElement tableLayoutElement = new TableLayoutElement(this.id);
        tableLayoutElement.waitUntilReady();
        return tableLayoutElement;
    }

    /**
     * @return a cart layout page object for the live data
     */
    public CardLayoutElement getCardLayout()
    {
        return new CardLayoutElement(this.id);
    }

    private void waitUntilReady()
    {
        // First the Live Data macro displays a simple div with the loading class.
        // This div is replaced by the Live Data Vue template once vue is loaded.
        getDriver().waitUntilCondition(
            input -> {
                String[] classes = getDriver().findElement(By.id(this.id)).getAttribute("class").split("\\s+");
                return !Arrays.asList(classes).contains("loading");
            });

        // Then, once the Vue template is loaded, a div with the loading class is inserted until the rest of the data 
        // and components required to display the Live Data are loaded too. 
        getDriver().waitUntilCondition(
            input -> getDriver().findElement(By.id(this.id)).findElements(By.cssSelector(".xwiki-livedata .loading"))
                .isEmpty()
        );
    }
}

@@ -981,16 +981,126 @@
 ium.By;%0A
+import org.openqa.selenium.NoSuchElementException;%0Aimport org.openqa.selenium.StaleElementReferenceException;%0A
 import o
@@ -2465,26 +2465,76 @@
     
-String%5B%5D classes =
+try %7B%0A                    String%5B%5D classes =%0A                       
  get
@@ -2545,32 +2545,46 @@
 er().findElement
+WithoutWaiting
 (By.id(this.id))
@@ -2605,16 +2605,45 @@
 %22class%22)
+%0A                            
 .split(%22
@@ -2646,24 +2646,28 @@
 it(%22%5C%5Cs+%22);%0A
+    
             
@@ -2718,24 +2718,498 @@
 %22loading%22);%0A
+                %7D catch (NoSuchElementException %7C StaleElementReferenceException e) %7B%0A                    // If there is no such element that mean the Live data is not loaded yet (or is missing).%0A                    // If the element is stale, that means the element was removed from the DOM in the meantime, because%0A                    // the initial div produced by the live data macro was replaced by the Vue template.%0A                    return false;%0A                %7D%0A
             

Fix unhandled StaleElementReferenceException on LiveDataElement.waitUntilReady