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
026import java.time.Duration;
027
028/**
029 * Actions available on the Edit page.
030 */
031public class EditWikiPage implements HaddockPage {
032
033    private static final String EDIT_TEXTAREA = "textarea.editor.form-control.snipeable";
034
035    /**
036     * Open a given page for edition.
037     *
038     * @param pageName Wiki page name to Edit.
039     * @return {@link EditWikiPage} instance, to allow chaining of actions.
040     */
041    public static EditWikiPage open( final String pageName ) {
042        return Page.withUrl( Page.baseUrl() + "/Edit.jsp?page=" + pageName ).openAs( new EditWikiPage() );
043    }
044
045    /**
046     * Press the cancel button and disacrd page Edit.
047     *
048     * @return {@link ViewWikiPage} instance, to allow chaining of actions.
049     */
050    public ViewWikiPage cancel() {
051        Selenide.$( By.name( "cancel" ) ).click();
052        return new ViewWikiPage();
053    }
054
055    /**
056     * Edits the page with the given text. Ensures edition is complete by ensuring the preview pane shows the edited text.
057     *
058     * @param text text to edit.
059     * @return {@link ViewWikiPage} instance, to allow chaining of actions.
060     */
061    public ViewWikiPage saveText(final String text ) {
062        return saveText( text, text );
063    }
064
065    /**
066     * Edits the page with the given text. Ensures edition is complete by ensuring the preview pane shows the preview text.
067     *
068     * @param text text text to edit.
069     * @param preview expected text to hsow up on the preview pane (i.e., page directives on edit pane shouldn't show up here).
070     * @return {@link ViewWikiPage} instance, to allow chaining of actions.
071     */
072    public ViewWikiPage saveText(final String text, final String preview ) {
073        Selenide.$( By.cssSelector( EDIT_TEXTAREA ) ).clear();
074        Selenide.$( By.cssSelector( EDIT_TEXTAREA ) ).val( text );
075        Selenide.$( By.className( "ajaxpreview" ) ).shouldBe( Condition.text( preview ), Duration.ofSeconds( 1L ) );
076        Selenide.$( By.name( "ok" ) ).click();
077        return new ViewWikiPage();
078    }
079
080}