001/*
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.
018 */
019package org.apache.wiki.pages.haddock;
020
021import com.codeborne.selenide.Condition;
022import com.codeborne.selenide.Selenide;
023import org.apache.wiki.pages.Page;
024import org.openqa.selenium.By;
025
026/**
027 * Actions available on the Edit page.
028 */
029public class EditWikiPage implements HaddockPage {
030
031    private static final String EDIT_TEXTAREA = "textarea.editor.form-control.snipeable";
032
033    /**
034     * Open a given page for edition.
035     *
036     * @param pageName Wiki page name to Edit.
037     * @return {@link EditWikiPage} instance, to allow chaining of actions.
038     */
039    public static EditWikiPage open( final String pageName ) {
040        return Page.withUrl( Page.baseUrl() + "/Edit.jsp?page=" + pageName ).openAs( new EditWikiPage() );
041    }
042
043    /**
044     * Press the cancel button and disacrd page Edit.
045     *
046     * @return {@link ViewWikiPage} instance, to allow chaining of actions.
047     */
048    public ViewWikiPage cancel() {
049        Selenide.$( By.name( "cancel" ) ).click();
050        return new ViewWikiPage();
051    }
052
053    /**
054     * Edits the page with the given text. Ensures edition is complete by ensuring the preview pane shows the edited text.
055     *
056     * @param text text to edit.
057     * @return {@link ViewWikiPage} instance, to allow chaining of actions.
058     */
059    public ViewWikiPage saveText(final String text ) {
060        return saveText( text, text );
061    }
062
063    /**
064     * Edits the page with the given text. Ensures edition is complete by ensuring the preview pane shows the preview text.
065     *
066     * @param text text text to edit.
067     * @param preview expected text to hsow up on the preview pane (i.e., page directives on edit pane shouldn't show up here).
068     * @return {@link ViewWikiPage} instance, to allow chaining of actions.
069     */
070    public ViewWikiPage saveText(final String text, final String preview ) {
071        Selenide.$( By.cssSelector( EDIT_TEXTAREA ) ).clear();
072        Selenide.$( By.cssSelector( EDIT_TEXTAREA ) ).val( text );
073        Selenide.$( By.className( "ajaxpreview" ) ).waitUntil( Condition.text( preview ), 1_000L );
074        Selenide.$( By.name( "ok" ) ).click();
075        return new ViewWikiPage();
076    }
077
078}